From 83fbcf0dfc2564813752badb2c3cf9846036b033 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:07:34 +0100 Subject: [PATCH] Test masked array (#8396) * Test masked array * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update test_namedarray.py * Update core.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- xarray/namedarray/core.py | 13 ++++++------- xarray/tests/test_namedarray.py | 8 ++++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index 2fef1cad3db..d3fcffcfd9e 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -40,7 +40,6 @@ from xarray.core.types import Dims from xarray.namedarray._typing import ( - DuckArray, _AttrsLike, _Chunks, _Dim, @@ -144,9 +143,9 @@ def _new( @overload def from_array( dims: _DimsLike, - data: DuckArray[_ScalarType], + data: duckarray[_ShapeType, _DType], attrs: _AttrsLike = ..., -) -> _NamedArray[_ScalarType]: +) -> NamedArray[_ShapeType, _DType]: ... @@ -155,15 +154,15 @@ def from_array( dims: _DimsLike, data: ArrayLike, attrs: _AttrsLike = ..., -) -> _NamedArray[Any]: +) -> NamedArray[Any, Any]: ... def from_array( dims: _DimsLike, - data: DuckArray[_ScalarType] | ArrayLike, + data: duckarray[_ShapeType, _DType] | ArrayLike, attrs: _AttrsLike = None, -) -> _NamedArray[_ScalarType] | _NamedArray[Any]: +) -> NamedArray[_ShapeType, _DType] | NamedArray[Any, Any]: """ Create a Named array from an array-like object. @@ -184,7 +183,7 @@ def from_array( "Array is already a Named array. Use 'data.data' to retrieve the data array" ) - # TODO: dask.array.ma.masked_array also exists, better way? + # TODO: dask.array.ma.MaskedArray also exists, better way? if isinstance(data, np.ma.MaskedArray): mask = np.ma.getmaskarray(data) # type: ignore[no-untyped-call] if mask.any(): diff --git a/xarray/tests/test_namedarray.py b/xarray/tests/test_namedarray.py index 448e8cf819a..e0141e12755 100644 --- a/xarray/tests/test_namedarray.py +++ b/xarray/tests/test_namedarray.py @@ -290,10 +290,14 @@ def test_duck_array_typevar(a: duckarray[Any, _DType]) -> duckarray[Any, _DType] numpy_a: NDArray[np.int64] numpy_a = np.array([2.1, 4], dtype=np.dtype(np.int64)) + test_duck_array_typevar(numpy_a) + + masked_a: np.ma.MaskedArray[Any, np.dtype[np.int64]] + masked_a = np.ma.asarray([2.1, 4], dtype=np.dtype(np.int64)) # type: ignore[no-untyped-call] + test_duck_array_typevar(masked_a) + custom_a: CustomArrayIndexable[Any, np.dtype[np.int64]] custom_a = CustomArrayIndexable(numpy_a) - - test_duck_array_typevar(numpy_a) test_duck_array_typevar(custom_a) # Test numpy's array api: