-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
TYP: ndarray.item
is defaulting to output string
#28017
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
Comments
The return type of
|
Yes, it runs smoothly but Mypy is recognizing it as string rather than an integer Error: |
Can you give a minimal example of some code where mypy behaves different in numpy 2.2 compared to earlier versions? |
This recreates effectively a similar case: |
While I have failed to recreate this error with your code (it prints 1), my theory is that the array might be of type np.int32 or np.int64 which is not the same thing as a python int and that's why mypy throws the error. |
ndarray.item
is defaulting to output stringndarray.item
is defaulting to output string
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
I can't reproduce this with the latest mypy (1.13.0): contents of from typing import reveal_type
import numpy as np
a = np.array("s", dtype=str)
reveal_type(a)
reveal_type(a.item())
b = np.array(10)
reveal_type(b)
reveal_type(b.item()) output of
relevant dependencies = [
"numpy>=2.2.0",
"mypy[faster-cache]>=1.13.0",
]
[tool.mypy]
modules = ["src/*"]
plugins = ["numpy.typing.mypy_plugin"]
python_version = "3.12"
strict = true So both What |
I confirmed that in the case where the scalar-type isn't from typing import reveal_type
import numpy as np
import numpy.typing as npt
c: npt.NDArray[np.int32]
reveal_type(c)
reveal_type(c.item())
|
If you look at the first In the other cases, the static type-checker will skip them, and use the |
@deathblade287 The issue is typing and mypy related, it is not a bug that will make the script fail at execution
giving output
However, when I run mypy on the script (1.13.0):
So it may be something to do with the way mypy gets the types which I do not know about, perhaps related to the bug you mentioned earlier. Edit: As per pranali's comment, this case does not occur if using npt.NDArray[Any] instead. Thanks @jorenham @PranaliPatil12 |
While looking at Anuj's case, I think using the Thank you @jorenham |
The only difference I see between your example here, and my earlier example (the one with But unfortunately I don't think that there's a workaround, or at least, not one that doesn't have severe negative side-effects (i.e. the " So I'd indeed classify this one as (yet another) "mypy bug without reasonable workaround". |
Describe the issue:
In version 2.2.0, the return value of
ndarray.item
is defaulting to str. The previous version the function returned int.I am not sure if it should be doing that.
I was following the documentation here but the new version 2.2.0 returns a string.
numpy/numpy/__init__.pyi
Lines 2111 to 2122 in a2012ad
The text was updated successfully, but these errors were encountered: