-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(types) Numpy.typing.NDArray #5212
base: master
Are you sure you want to change the base?
feat(types) Numpy.typing.NDArray #5212
Conversation
I don't know enough about typing to meaningfully weigh the pros and cons of changing from Regarding the pybind11/include/pybind11/typing.h Lines 138 to 142 in 51c2aa1
Is your change needed anyway? Could you send a separate, small PR for that, including a test that fails without your change? |
NDArray is the public type for NDArrays, and hides the "size" related type (as described above). It also uses a different form for the DType. Just curious, what about changing it to FYI, I get an error on your example: $ bat tmp.py
───────┬─────────────────────────────────────────
│ File: tmp.py
───────┼─────────────────────────────────────────
1 │ import numpy
2 │ from typing import Any
3 │
4 │ v: numpy.typing.NDArray[numpy.float64]
5 │ reveal_type(v)
6 │ reveal_type(v.dtype)
7 │
8 │ q: numpy.ndarray[Any, numpy.float64]
9 │ reveal_type(q)
10 │ reveal_type(q.dtype)
11 │
12 │
───────┴─────────────────────────────────────────
$ pipx install mypy
$ pipx inject mypy numpy
$ mypy tmp.py
tmp.py:5: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._64Bit]]]"
tmp.py:6: note: Revealed type is "numpy.dtype[numpy.floating[numpy._typing._64Bit]]"
tmp.py:8: error: Type argument "floating[_64Bit]" of "ndarray" must be a subtype of "dtype[Any]" [type-var]
tmp.py:9: note: Revealed type is "numpy.ndarray[Any, numpy.floating[numpy._typing._64Bit]]"
tmp.py:10: note: Revealed type is "numpy.floating[numpy._typing._64Bit]"
Found 1 error in 1 file (checked 1 source file) |
In my example I have |
Description
Switches to using
numpy.typing.NDArray
for typing annotations overnumpy.ndarray
. This is becausenumpy.ndarray
is implemented with the first argument for some future size annotation. See numpy/numpy#16544. Since the first argument is for the size annotation the typing of thenumpy.ndarray
is basically not being used. I also modified the eigen matrix/tensor tonumpy.typing.NDArray
as well even though stubgen fails to generate stubs sincenumpy.typing.NDArray[numpy.float64[...]]
is not a valid type.Suggested changelog entry:
Switched to `numpy.typing.NDArray`