Skip to content

Commit

Permalink
Use Self becuase Variable subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Illviljan committed Oct 14, 2023
1 parent 3d24448 commit d126abc
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
from typing_extensions import Self

T_NamedArray = TypeVar("T_NamedArray", bound="_NamedArray[Any]")
T_NamedArrayInteger = TypeVar(
"T_NamedArrayInteger", bound="_NamedArray[np.integer[Any]]"
)


def _new(
Expand Down Expand Up @@ -467,7 +470,7 @@ def _replace(
The types for each argument cannot change,
use _new if that is a risk.
"""
return _new(self, dims, data, attrs)
return cast(Self, _new(self, dims, data, attrs))

def _copy(
self,
Expand Down Expand Up @@ -527,7 +530,7 @@ def copy(
"""
return self._copy(deep=deep, data=data)

def _nonzero(self) -> tuple[_NamedArray[np.integer[Any]], ...]:
def _nonzero(self: T_NamedArrayInteger) -> tuple[T_NamedArrayInteger, ...]:
"""Equivalent numpy's nonzero but returns a tuple of NamedArrays."""
# TODO: we should replace dask's native nonzero
# after https://github.com/dask/dask/issues/1076 is implemented.
Expand All @@ -542,7 +545,7 @@ def _as_sparse(
self,
sparse_format: Literal["coo"] | Default = _default,
fill_value: ArrayLike | Default = _default,
) -> NamedArray[Any, _DType_co]:
) -> Self:
"""
use sparse-array as backend.
"""
Expand All @@ -566,7 +569,7 @@ def _as_sparse(
data = as_sparse(astype(self, dtype).data, fill_value=fill_value)
return self._replace(data=data)

def _to_dense(self) -> NamedArray[Any, _DType_co]:
def _to_dense(self) -> Self:
"""
Change backend from sparse to np.array
"""
Expand Down

0 comments on commit d126abc

Please sign in to comment.