Skip to content
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

mypy failures with numpy>=2 #9231

Closed
dcherian opened this issue Jul 11, 2024 · 4 comments
Closed

mypy failures with numpy>=2 #9231

dcherian opened this issue Jul 11, 2024 · 4 comments

Comments

@dcherian
Copy link
Contributor

dcherian commented Jul 11, 2024

What happened?

Mypy fails on main with numpy >= 2

xarray/core/npcompat.py: note: In function "isdtype":
xarray/core/npcompat.py:48: error: All conditional function variants must have identical signatures  [misc]
xarray/core/npcompat.py:48: note: Original:
xarray/core/npcompat.py:48: note:     def isdtype(dtype: dtype[Any] | type[Any], kind: dtype[Any] | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] | None | tuple[dtype[Any] | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] | None, ...]) -> bool
xarray/core/npcompat.py:48: note: Redefinition:
xarray/core/npcompat.py:48: note:     def isdtype(dtype: Any, kind: Any) -> Any
xarray/core/dataset.py:34: error: Module "numpy" has no attribute "RankWarning"  [attr-defined]
xarray/core/dataset.py:34: error: Name "RankWarning" already defined (possibly by an import)  [no-redef]
xarray/tests/test_namedarray.py: note: In class "TestNamedArray":
xarray/tests/test_namedarray.py:183: error: Argument 2 to "NamedArray" has incompatible type "ndarray[Any, dtype[Any]]"; expected "_arrayfunction[Any, Never] | _arrayapi[Any, Never]"  [arg-type]
xarray/tests/test_namedarray.py: note: In member "test_duck_array_class" of class "TestNamedArray":
xarray/tests/test_namedarray.py:344: error: Argument 1 to "check_duck_array_typevar" has incompatible type "ndarray[Any, dtype[signedinteger[_64Bit]]]"; expected "_arrayfunction[Any, Never] | _arrayapi[Any, Never]"  [arg-type]
xarray/tests/test_namedarray.py: note: In member "test_warn_on_repeated_dimension_names" of class "TestNamedArray":
xarray/tests/test_namedarray.py:563: error: Argument 2 to "NamedArray" has incompatible type "ndarray[Any, dtype[signedinteger[Any]]]"; expected "_arrayfunction[Any, Never] | _arrayapi[Any, Never]"  [arg-type]
xarray/tests/test_strategies.py: note: In member "test_make_strategies_namespace" of class "TestVariablesStrategy":
xarray/tests/test_strategies.py:220: error: Module "numpy" has no attribute "array_api"  [attr-defined]
xarray/tests/test_strategies.py:220: note: Error code "attr-defined" not covered by "type: ignore" comment
xarray/tests/test_plot.py: note: In member "pass_in_axis" of class "PlotTestCase":
xarray/tests/test_plot.py:162: error: Value of type "Axes | ndarray[Any, Any]" is not indexable  [index]
xarray/tests/test_plot.py:163: error: Value of type "Axes | ndarray[Any, Any]" is not indexable  [index]
xarray/tests/test_plot.py: note: In member "test_1d_x_y_kw" of class "TestPlot":
xarray/tests/test_plot.py:245: error: Item "Axes" of "Axes | ndarray[Any, Any]" has no attribute "flat"  [union-attr]
xarray/tests/test_plot.py: note: In member "test_colorbar_kwargs" of class "Common2dMixin":
xarray/tests/test_plot.py:1569: error: "Axes" object is not iterable  [misc]
xarray/tests/test_plot.py:1579: error: "Axes" object is not iterable  [misc]
xarray/tests/test_plot.py: note: In function "test_plot1d_default_rcparams":
xarray/tests/test_plot.py:3375: error: Argument 1 to "assert_allclose" has incompatible type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]"; expected "_SupportsArray[dtype[numpy.bool | number[Any]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool | number[Any]]]] | builtins.bool | int | float | complex | _NestedSequence[builtins.bool | int | float | complex] | _SupportsArray[dtype[object_]] | _NestedSequence[_SupportsArray[dtype[object_]]]"  [arg-type]
xarray/tests/test_plot.py:3382: error: Argument 1 to "assert_allclose" has incompatible type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]"; expected "_SupportsArray[dtype[numpy.bool | number[Any]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool | number[Any]]]] | builtins.bool | int | float | complex | _NestedSequence[builtins.bool | int | float | complex] | _SupportsArray[dtype[object_]] | _NestedSequence[_SupportsArray[dtype[object_]]]"  [arg-type]
xarray/tests/test_plot.py:3394: error: Argument 1 to "assert_allclose" has incompatible type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]"; expected "_SupportsArray[dtype[numpy.bool | number[Any]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool | number[Any]]]] | builtins.bool | int | float | complex | _NestedSequence[builtins.bool | int | float | complex] | _SupportsArray[dtype[object_]] | _NestedSequence[_SupportsArray[dtype[object_]]]"  [arg-type]
xarray/tests/test_dataset.py:22: error: Module "numpy" has no attribute "RankWarning"  [attr-defined]
xarray/tests/test_dataset.py:22: error: Name "RankWarning" already defined (possibly by an import)  [no-redef]
xarray/tests/test_dataset.py:87: error: Module "numpy" has no attribute "trapz"  [attr-defined]
xarray/tests/test_dataarray.py:20: error: Module "numpy" has no attribute "RankWarning"  [attr-defined]
xarray/tests/test_dataarray.py:20: error: Name "RankWarning" already defined (possibly by an import)  [no-redef]
@TomNicholas
Copy link
Member

xarray/tests/test_strategies.py:220: error: Module "numpy" has no attribute "array_api" [attr-defined]

This one is because numpy removed their experimental np.array_api namespace. It should be fixable simply by replacing it with numpy's main namespace. See

https://numpy.org/doc/stable/reference/array_api.html

@Illviljan
Copy link
Contributor

xarray/tests/test_strategies.py:220: error: Module "numpy" has no attribute "array_api" [attr-defined]

This one is because numpy removed their experimental np.array_api namespace. It should be fixable simply by replacing it with numpy's main namespace. See

https://numpy.org/doc/stable/reference/array_api.html

Should be a little careful about this. I don't know test_strategies.py that well but np.array_api was a strict implementation of the array api, if we still want a strict implementation there's https://github.com/data-apis/array-api-strict.

@keewis
Copy link
Collaborator

keewis commented Jul 12, 2024

this is the relevant part of the code:

if Version(np.__version__) >= Version("2.0.0.dev0"):
nxp = np
else:
# requires numpy>=1.26.0, and we expect a UserWarning to be raised
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", category=UserWarning, message=".+See NEP 47."
)
from numpy import ( # type: ignore[no-redef,unused-ignore]
array_api as nxp,
)

We already use numpy.array_api only on numpy<2.0, but mypy doesn't appear to understand the version check. So we might just have to add attr-defined to the type ignores?

The RankWarning (and trapz) redefinitions are also compatibility code, so might also need an ignore.

@headtr1ck
Copy link
Collaborator

Closed by #9252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants