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: np.mod(npt.NDArray, Any) is Any in 2.2.2 while it wasn't in 2.2.1 #28192
numpy 2.2.2 reveals a different type for np.mod(npt.NDArray, Any) than 2.2.1. This looks like a result of #28176. Please close if this is an intended change.
This actually wasn't intentional. I'm able to reproduce this, but only with mypy, and e.g. Pyright reports the following:
Type of "np.mod(x, y)" is "ndarray[tuple[int, ...], dtype[Any]]"
But I can't explain why mypy is behaving this way. There's only one overload that return Any, and that's the first one. But as you can see, it only accepts scalar-like input, so it shouldn't be relevant in this case.
So I would have expected mypy to use the 2nd or 3rd overload instead, and return NDArray[Any], like it did before. Even if it went for the last overload, the return type would be NDArray[Any] | Any, which isn't the same as Any (T | Any is not reducible to Any, according to the typing spec, and is defined to have some specific meaning).
But either way, for the majority of users, I don't expect that this will lead to any issues. In general, I would advise to avoid using Any if you can, as it can lead to undefined behavior, and using it is almost always type-unsafe.
So if you're alright with it, I'll close this issue, as it appears to be caused by a bug in mypy (and only mypy).
Activity
jorenham commentedon Jan 20, 2025
Thanks for reporting this!
This actually wasn't intentional. I'm able to reproduce this, but only with mypy, and e.g. Pyright reports the following:
But I can't explain why mypy is behaving this way. There's only one overload that return
Any, and that's the first one. But as you can see, it only accepts scalar-like input, so it shouldn't be relevant in this case.numpy/numpy/_typing/_ufunc.pyi
Lines 175 to 228 in fd8a68e
So I would have expected mypy to use the 2nd or 3rd overload instead, and return
NDArray[Any], like it did before. Even if it went for the last overload, the return type would beNDArray[Any] | Any, which isn't the same asAny(T | Anyis not reducible toAny, according to the typing spec, and is defined to have some specific meaning).But either way, for the majority of users, I don't expect that this will lead to any issues. In general, I would advise to avoid using
Anyif you can, as it can lead to undefined behavior, and using it is almost always type-unsafe.So if you're alright with it, I'll close this issue, as it appears to be caused by a bug in mypy (and only mypy).
np.maximum(np.array(1), np.array(1))isAnyin 2.2.2 while it wasn't in 2.2.1 #28193 as a duplicate of this issue