Skip to content

Commit

Permalink
Update signature for _arrayfunction.__array__ (#9237)
Browse files Browse the repository at this point in the history
* Update test_namedarray.py

* Update _typing.py

* Update test_namedarray.py

* Update test_namedarray.py

* Update _typing.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update _typing.py

* Add shapetypes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix __array__ missing copy parameter

* try reverting shapetypes

* try reverting shapetypes

* Update test_namedarray.py

* Update _typing.py

---------

Co-authored-by: Maximilian Roos <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 21, 2024
1 parent 91a52dc commit 10bb94c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions xarray/namedarray/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ def __getitem__(
) -> _arrayfunction[Any, _DType_co] | Any: ...

@overload
def __array__(self, dtype: None = ..., /) -> np.ndarray[Any, _DType_co]: ...

def __array__(
self, dtype: None = ..., /, *, copy: None | bool = ...
) -> np.ndarray[Any, _DType_co]: ...
@overload
def __array__(self, dtype: _DType, /) -> np.ndarray[Any, _DType]: ...
def __array__(
self, dtype: _DType, /, *, copy: None | bool = ...
) -> np.ndarray[Any, _DType]: ...

def __array__(
self, dtype: _DType | None = ..., /
self, dtype: _DType | None = ..., /, *, copy: None | bool = ...
) -> np.ndarray[Any, _DType] | np.ndarray[Any, _DType_co]: ...

# TODO: Should return the same subclass but with a new dtype generic.
Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_init(self, expected: Any) -> None:
# Fail:
(
("x",),
NamedArray("time", np.array([1, 2, 3])), # type: ignore
NamedArray("time", np.array([1, 2, 3], dtype=np.dtype(np.int64))),
np.array([1, 2, 3]),
True,
),
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_dims_setter(
def test_duck_array_class(self) -> None:
numpy_a: NDArray[np.int64]
numpy_a = np.array([2.1, 4], dtype=np.dtype(np.int64))
check_duck_array_typevar(numpy_a) # type: ignore
check_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]
Expand Down Expand Up @@ -560,4 +560,4 @@ def test_broadcast_to_errors(

def test_warn_on_repeated_dimension_names(self) -> None:
with pytest.warns(UserWarning, match="Duplicate dimension names"):
NamedArray(("x", "x"), np.arange(4).reshape(2, 2)) # type: ignore
NamedArray(("x", "x"), np.arange(4).reshape(2, 2))

0 comments on commit 10bb94c

Please sign in to comment.