Skip to content

Commit

Permalink
Update _typing.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Illviljan committed Jul 9, 2024
1 parent 0ec9be7 commit 1e35da3
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions xarray/namedarray/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ def dtype(self) -> _DType_co: ...


@runtime_checkable
class _arrayfunction(
_array[_ShapeType_co, _DType_co], Protocol[_ShapeType_co, _DType_co]
):
class _arrayfunction(_array[_ShapeType, _DType_co], Protocol[_ShapeType, _DType_co]):
"""
Duck array supporting NEP 18.
Expand All @@ -185,14 +183,14 @@ def __getitem__(
) -> _arrayfunction[Any, _DType_co] | Any: ...

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

@overload
def __array__(self, dtype: _DType, /) -> np.ndarray[Any, _DType]: ...
def __array__(self, dtype: _DType, /) -> np.ndarray[_ShapeType, _DType]: ...

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

# TODO: Should return the same subclass but with a new dtype generic.
# https://github.com/python/typing/issues/548
Expand All @@ -215,10 +213,10 @@ def __array_function__(
) -> Any: ...

@property
def imag(self) -> _arrayfunction[_ShapeType_co, Any]: ...
def imag(self) -> _arrayfunction[_ShapeType, Any]: ...

@property
def real(self) -> _arrayfunction[_ShapeType_co, Any]: ...
def real(self) -> _arrayfunction[_ShapeType, Any]: ...


@runtime_checkable
Expand Down Expand Up @@ -267,7 +265,7 @@ def chunks(self) -> _Chunks: ...

@runtime_checkable
class _chunkedarrayfunction(
_arrayfunction[_ShapeType_co, _DType_co], Protocol[_ShapeType_co, _DType_co]
_arrayfunction[_ShapeType, _DType_co], Protocol[_ShapeType, _DType_co]
):
"""
Chunked duck array supporting NEP 18.
Expand All @@ -281,7 +279,7 @@ def chunks(self) -> _Chunks: ...
def rechunk(
self,
chunks: _ChunksLike,
) -> _chunkedarrayfunction[_ShapeType_co, _DType_co]: ...
) -> _chunkedarrayfunction[_ShapeType, _DType_co]: ...


@runtime_checkable
Expand Down Expand Up @@ -326,7 +324,7 @@ def todense(self) -> np.ndarray[Any, np.dtype[np.generic]]: ...

@runtime_checkable
class _sparsearrayfunction(
_arrayfunction[_ShapeType_co, _DType_co], Protocol[_ShapeType_co, _DType_co]
_arrayfunction[_ShapeType, _DType_co], Protocol[_ShapeType, _DType_co]
):
"""
Sparse duck array supporting NEP 18.
Expand Down Expand Up @@ -361,8 +359,18 @@ def todense(self) -> np.ndarray[Any, np.dtype[np.generic]]: ...
ErrorOptionsWithWarn = Literal["raise", "warn", "ignore"]


def test(arr: duckarray[_ShapeType, _DType]) -> duckarray[_ShapeType, _DType]:
return arr
# def test(arr: duckarray[_ShapeType, _DType]) -> duckarray[_ShapeType, _DType]:
# return np.round(arr)


# test(np.array([], dtype=np.int64))


# def test2(arr: _arrayfunction[Any, _DType]) -> _arrayfunction[Any, _DType]:
# return np.round(arr)
# # return np.asarray(arr)
# # return arr.__array__()
# # return arr


test(np.array([], dtype=np.int64))
# test2(np.array([], dtype=np.int64))

0 comments on commit 1e35da3

Please sign in to comment.