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
It seems that logical_or is annotated to return "ndarray[tuple[int, ...], dtype[Any]]", but it returns a single bool when called with scalar arguments. Probably other logical_xxx functions show the same behavior.
I could try to fix it (Returning a union of bool and the current annotation), but need someone pointing me at the location of the annotation.
This error is caused by a limitation in mypy's type inference system, and won't occur with other typecheckers like pyright or basedpyright. See #27957 for a (rather lengthy) discussion on this in detail.
This helps mypy to understand what the common denominator of in_SST_ is, because it isn't able to figure that it by itself.
If possible for you, you might also want to consider switching to pyright or basedpyright, which don't have this issue.
But even so, you're right in saying that the return type of logical_or is too narrow. Because, like np.all, its return type should also include to possibility for scalars, not only arrays.
But as you can see in
, the second that accepts any "array-like" (which includes scalars), is annotated to return NDArray[Any]. If a union of scalar and array is passed, we get an incorrect result.
Somehow this has gone by unnoticed for 4 years, so thanks for reporting this!
@jorenham
thanks for the quick reply and the detailed explanation.
Actually, I think everything is correct for the typehint in line 190, at least for the case I had in mind. If only one of the arguments of np.logical_and is a scalar, the return value is an array. Only two scalars (the other @overload) will give a scalar. The problem is just that mypy apparently infers the type from the first assignment instead of checking all assignments for a common denominator.
Activity
jorenham commentedon Jan 16, 2025
This error is caused by a limitation in mypy's type inference system, and won't occur with other typecheckers like pyright or basedpyright. See #27957 for a (rather lengthy) discussion on this in detail.
Luckily, there's an easy workaround:
This helps mypy to understand what the common denominator of
in_SST_is, because it isn't able to figure that it by itself.If possible for you, you might also want to consider switching to pyright or basedpyright, which don't have this issue.
But even so, you're right in saying that the return type of
logical_oris too narrow. Because, likenp.all, its return type should also include to possibility for scalars, not only arrays.But as you can see in
numpy/numpy/_typing/_ufunc.pyi
Lines 163 to 190 in 1e10174
NDArray[Any]. If a union of scalar and array is passed, we get an incorrect result.Somehow this has gone by unnoticed for 4 years, so thanks for reporting this!
Some related issues:
jorenham commentedon Jan 16, 2025
FYI, there's a newer
v1.14.1mypy release availableMarDiehl commentedon Jan 16, 2025
@jorenham
thanks for the quick reply and the detailed explanation.
Actually, I think everything is correct for the typehint in line 190, at least for the case I had in mind. If only one of the arguments of
np.logical_andis a scalar, the return value is an array. Only two scalars (the other@overload) will give a scalar. The problem is just that mypy apparently infers the type from the first assignment instead of checking all assignments for a common denominator.jorenham commentedon Jan 16, 2025
The bug is rather subtle:
So
outis inferred as array, even though both inputs could also be a scalar.