Type stub integral and floating points bindings to accept the corresponding numpy types #983
Replies: 3 comments 5 replies
-
Note that the C++ function I'm not at all knowledgeable about the Python typing stuff. Maybe it's good that a typing tool complains about automatic conversions that may hurt accuracy? Again, I don't know the right answer. Though, I would consider preferring the C++ function |
Beta Was this translation helpful? Give feedback.
-
In your own personal project, https://nanobind.readthedocs.io/en/latest/typing.html#pattern-files might be a good solution. I would suggest that the general problem of defining a type system for number-like types is not something that can or should be undertaken by nanobind. For those interested in element types of Python arrays, the following may be of interest: |
Beta Was this translation helpful? Give feedback.
-
Those interested in this discussion and in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If I bind
float my_func(float x)
I want the type stubs to bemy_func(x: float | np.float32 | np.float64) -> float
. I can pass annp.float32
at run time without issue, butisinstance(np.float32, float)
evaluates false so type checkers throw an error.I see exactly what I need to change here:
nanobind/include/nanobind/nb_cast.h
Line 187 in 9b3afa9
In my own personal project I'm hacking in the necessary
numpy
imports with__prefix__
statements, but I think I could figure out how to fix the required imports properly.@wjakob are you interested in making the typing changes part of the master branch? We can avoid users to have
numpy
installed withif TYPE_CHECKING
blocks around the import statement. If not, do you have any recommendations for how I override thetype_caster
? I know I can copy all ofnb_cast.h
and make my own version, but maintaining my own fork of that file doesn't sound like a great solution.Beta Was this translation helpful? Give feedback.
All reactions