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
Numpy 2.2 introduced new return type semantics for np.dtype. Mypy is identifying the return type of np.dtype(Any) as Any. Example and mypy logs below 👇
❯ mypy ./test.py
/Users/jhamman/scratch/test.py:5: note: Revealed type is "Any"
/Users/jhamman/scratch/test.py:6: 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.11.6 | packaged by conda-forge | (main, Oct 3 2023, 10:37:07) [Clang 15.0.7 ]
This did seem to work fine in Numpy 2.1 so smells like I regression to me.
pip install -U numpy==2.1
mypy ~/Downloads/test.py
/Users/jhamman/scratch/test.py:5: note: Revealed type is "numpy.dtype[Any]"
Success: no issues found in 1 source file
[...]/issue_28034.pyi:6: note: Revealed type is "Any"
Success: no issues found in 1 source file
output of uv run pyright [...]/issue_28034.pyi:
[...]/issue_28034.pyi
[...]/issue_28034.pyi:6:13 - information: Type of "np.dtype(no_idea_what_this_is)" is "dtype[Unknown]"
0 errors, 0 warnings, 1 information
So this appears to be yet another mypy bug 😑. And in this case I can't think of any way to work around it without breaking other things.
FYI, this is another example of why you shouldn't use Any if you can avoid it, and one of the (many) reasons for why tools like flake8-pyi and basedpyright report usages of Any as an error.
Ah thanks – from the discussion above, I thought that the np.dtype(dtype) line would be problematic because dtype is Any and np.dtype does not have any overloads for that. But perhaps I misinterpreted things.
Ah thanks – from the discussion above, I thought that the np.dtype(dtype) line would be problematic because dtype is Any and np.dtype does not have any overloads for that. But perhaps I misinterpreted things.
Yea that's understandable. Any is just very annoying to deal with, and doesn't behave like any other type (pun intended), as everything can be assigned to Any, and Any can be assigned to everything.
/Users/jhamman/scratch/test.py:5: note: Revealed type is "Any"
/Users/jhamman/scratch/test.py:6: error: Returning Any from function declared to return "dtype[generic[Any]]" [no-any-return]
/Users/jhamman/scratch/test.py:5: note: Revealed type is "Any"
/Users/jhamman/scratch/test.py:6: error: Returning Any from function declared to return "dtype[generic[Any]]" [no-any-return]
That check isn't enabled by default if I remember correctly. It's called disallow_any_explicit or disallow_any_expr I believe
Activity
jakevdp commentedon Dec 18, 2024
It looks like there's no overload that matches
np.dtype.__new__(cls, dtype: Any):numpy/numpy/__init__.pyi
Line 1167 in 5a2da5e
jhamman commentedon Dec 18, 2024
Thanks @jakevdp! Nice to bump into you again :)
This did seem to work fine in Numpy 2.1 so smells like I regression to me.
jakevdp commentedon Dec 18, 2024
The problematic change is likely #27139
[-]Return type of np.dtype is Any?[/-][+]TYP: Return type of `np.dtype` is `Any`?[/+]jorenham commentedon Dec 19, 2024
What did you expect would be the outcome?
jorenham commentedon Dec 19, 2024
That's intentional, because otherwise something invalid like
np.dtype(NotImplemented)would result in a false negative.jorenham commentedon Dec 19, 2024
repro:
relevant
pyproject.tomlconfig:output of
uv run mypy [...]/issue_28034.pyi:output of
uv run pyright [...]/issue_28034.pyi:So this appears to be yet another mypy bug 😑. And in this case I can't think of any way to work around it without breaking other things.
FYI, this is another example of why you shouldn't use
Anyif you can avoid it, and one of the (many) reasons for why tools likeflake8-pyiandbasedpyrightreport usages ofAnyas an error.jakevdp commentedon Dec 19, 2024
Is there a valid annotation that means "any dtype"?
jorenham commentedon Dec 19, 2024
np.dtype[np.generic]jakevdp commentedon Dec 19, 2024
OK, so then am I correct in understanding that the recommended approach here would be something like this?
jorenham commentedon Dec 19, 2024
That works! Or even better, you could remove
# type: ignore, as this already is valid :)jakevdp commentedon Dec 19, 2024
Ah thanks – from the discussion above, I thought that the
np.dtype(dtype)line would be problematic becausedtypeisAnyandnp.dtypedoes not have any overloads for that. But perhaps I misinterpreted things.jorenham commentedon Dec 19, 2024
Yea that's understandable.
Anyis just very annoying to deal with, and doesn't behave like any other type (pun intended), as everything can be assigned toAny, andAnycan be assigned to everything.jhamman commentedon Dec 20, 2024
I don't think this is true.
jorenham commentedon Dec 20, 2024
That check isn't enabled by default if I remember correctly. It's called
disallow_any_explicitordisallow_any_exprI believejorenham commentedon Dec 20, 2024
Either way, even with that check enabled, if you replace the
dtype: Anywithdtype: npt.DTypeLikeit'll also work: