You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TYP: excessively vague return types from np.dtype and np.ndarray.dtype in numpy 2.2.0 #27945
While upgrading gpgi's CI to numpy 2.2.0, I got a new typechecking error that seems spurious to me: an array's dtype appears to not have a defined type (as seen by mypy).
No error is raised with numpy 2.1.3.
Reproduce the code example:
importnumpyasnpdefget_float_dtype(reference: np.ndarray) ->np.dtype:
dt=reference.dtypeifdt.kind!="f":
raiseValueError(f"Invalid data type {dt} (expected a float dtype)")
returnnp.dtype(dt)
# also tried withreturndt
Error message:
gpgi.py:7: error: Returning Any from functiondeclared to return"dtype[Any]" [no-any-return]
Found 1 error in 1 file (checked 1 source file)
Python and NumPy Versions:
2.2.0
3.13.1 (main, Dec 4 2024, 16:25:14) [Clang 16.0.0 (clang-1600.0.26.4)]
Type-checker version and settings:
mypy==1.13.0
the --warn-return-any flag is needed to reproduce
Additional typing packages.
Included with mypy, but not used here (as far as I'm aware)
reference: np.ndarray is invalid: it's missing the type arguments, which makes it impossible for type-checkers to infer the actual dtype that you're expecting it to have. Simlarly np.dtype is also missing its type argument for the scalar type.
Judging from your example, I believe you'd want to annotate it as something like:
Is this generally true starting in NumPy v2.2.0, that raw np.ndarray annotations without shape/dtype qualifiers are invalid? If so, that change invalidates a lot of existing code.
Is this generally true starting in NumPy v2.2.0, that raw np.ndarray annotations without shape/dtype qualifiers are invalid? If so, that change invalidates a lot of existing code.
Thanks a lot. I was able to fix the real life example my MRC was derived from (unfortunately I can't use your preferred solution, TypeVar, because the real code needs to accommodate for reference=None) !
It's news to me too. I'm culprit of sprinkling such annotations in many places back a couple years ago before np.typing existed, and I'm not sure how to fix most of them.
ndarray has always been invalid, but that gets hidden by type-checkers unless you're in strict mode. NumPy 2.2.0 won't make this "more invalid" or something, so no worries
Fun fact: NumPy 2.2.0 actually made things like np.floating explicitly valid by having it default to np.floating[Any]. See the release notes for details.
Thanks a lot. I was able to fix the real life example my MRC was derived from (unfortunately I can't use your preferred solution, TypeVar, because the real code needs to accommodate for reference=None) !
Activity
jorenham commentedon Dec 10, 2024
reference: np.ndarrayis invalid: it's missing the type arguments, which makes it impossible for type-checkers to infer the actual dtype that you're expecting it to have. Simlarlynp.dtypeis also missing its type argument for the scalar type.Judging from your example, I believe you'd want to annotate it as something like:
or even better:
This is working as intended.
jakevdp commentedon Dec 10, 2024
Is this generally true starting in NumPy v2.2.0, that raw
np.ndarrayannotations without shape/dtype qualifiers are invalid? If so, that change invalidates a lot of existing code.jorenham commentedon Dec 10, 2024
This has always been invalid.
neutrinoceros commentedon Dec 10, 2024
Thanks a lot. I was able to fix the real life example my MRC was derived from (unfortunately I can't use your preferred solution,
TypeVar, because the real code needs to accommodate forreference=None) !jorenham commentedon Dec 10, 2024
You could use
@overloadto split it up into a case forreference: Noneand another forreference: npt.NDArray[_FloatingT]jakevdp commentedon Dec 10, 2024
Well, that's news to me! It never resulted in an error before, which I suspect is why the pattern is used hundreds of thousands of times across github: https://github.com/search?q=%22%3A+np.ndarray%22&type=code
NumPy 2.2 is going to break a lot of code.
neutrinoceros commentedon Dec 10, 2024
It's news to me too. I'm culprit of sprinkling such annotations in many places back a couple years ago before np.typing existed, and I'm not sure how to fix most of them.
seberg commentedon Dec 10, 2024
Is there any way we can be gentle here and keep allowing whatever is the biggest "breaking" changing here?!
jorenham commentedon Dec 10, 2024
ndarrayhas always been invalid, but that gets hidden by type-checkers unless you're in strict mode. NumPy 2.2.0 won't make this "more invalid" or something, so no worriesjorenham commentedon Dec 10, 2024
There's no need to, unless you enable strict mode in your type-checker settings.
jorenham commentedon Dec 10, 2024
Fun fact: NumPy 2.2.0 actually made things like
np.floatingexplicitly valid by having it default tonp.floating[Any]. See the release notes for details.jorenham commentedon Dec 10, 2024
Closing this now as resolved.