Skip to content

Commit

Permalink
update generate_ops.py
Browse files Browse the repository at this point in the history
  • Loading branch information
andersy005 committed Oct 26, 2023
1 parent 7ac44e0 commit 3d1e881
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
11 changes: 2 additions & 9 deletions xarray/core/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
DataArrayOpsMixin,
DatasetGroupByOpsMixin,
DatasetOpsMixin,
VariableOpsMixin,
)
from xarray.core.common import ImplementsArrayReduce, ImplementsDatasetReduce
from xarray.core.ops import (
IncludeNumpySameMethods,
IncludeReduceMethods,
)
from xarray.core.ops import IncludeNumpySameMethods, IncludeReduceMethods
from xarray.core.options import OPTIONS, _get_keep_attrs
from xarray.core.pycompat import is_duck_array

Expand Down Expand Up @@ -97,10 +93,7 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):


class VariableArithmetic(
ImplementsArrayReduce,
IncludeNumpySameMethods,
SupportsArithmetic,
VariableOpsMixin,
ImplementsArrayReduce, IncludeNumpySameMethods, SupportsArithmetic
):
__slots__ = ()
# prioritize our operations over those of numpy.ndarray (priority=0)
Expand Down
2 changes: 1 addition & 1 deletion xarray/namedarray/_typed_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from xarray.core import nputils, ops

if TYPE_CHECKING:
from xarray.namedarray.utils import NamedArrayCompatible, Self
from xarray.namedarray._typing import NamedArrayCompatible, Self


class NamedArrayOpsMixin:
Expand Down
19 changes: 18 additions & 1 deletion xarray/namedarray/_typing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from collections.abc import Hashable, Iterable, Mapping, Sequence
from types import ModuleType
from typing import (
Expand All @@ -14,10 +15,22 @@
runtime_checkable,
)

if sys.version_info >= (3, 11):
from typing import Self # noqa
else:
from typing_extensions import Self # noqa

import numpy as np

if TYPE_CHECKING:
from numpy.typing import NDArray
from numpy.typing import ArrayLike, NDArray

from xarray.namedarray.core import NamedArray

try:
from dask.array import Array as DaskArray
except ImportError:
DaskArray = np.ndarray # type: ignore


# https://stackoverflow.com/questions/74633074/how-to-type-hint-a-generic-numpy-array
Expand Down Expand Up @@ -263,3 +276,7 @@ def todense(self) -> NDArray[_ScalarType_co]:

# NamedArray can most likely use both __array_function__ and __array_namespace__:
_sparsearrayfunction_or_api = (_sparsearrayfunction, _sparsearrayapi)


ScalarOrArray = Union["ArrayLike", np.generic, np.ndarray, "DaskArray"]
NamedArrayCompatible = Union["NamedArray", "ScalarOrArray"]
5 changes: 4 additions & 1 deletion xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# TODO: get rid of this after migrating this class to array API
from xarray.core import dtypes, formatting, formatting_html
from xarray.namedarray._aggregations import NamedArrayAggregations
from xarray.namedarray._typed_ops import NamedArrayOpsMixin
from xarray.namedarray._typing import (
_arrayapi,
_arrayfunction_or_api,
Expand Down Expand Up @@ -203,7 +204,9 @@ def from_array(
return NamedArray(dims, np.asarray(data), attrs)


class NamedArray(NamedArrayAggregations, Generic[_ShapeType_co, _DType_co]):
class NamedArray(
NamedArrayAggregations, NamedArrayOpsMixin, Generic[_ShapeType_co, _DType_co]
):
"""
A wrapper around duck arrays with named dimensions
and attributes which describe a single Array.
Expand Down
3 changes: 2 additions & 1 deletion xarray/util/generate_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ def _render_classbody(method_blocks: list[OpsType]) -> Iterator[str]:
from typing import TYPE_CHECKING, Any, Callable
from xarray.core import nputils, ops
if TYPE_CHECKING:
from xarray.namedarray.utils import NamedArrayCompatible, Self'''
from xarray.namedarray._typing import NamedArrayCompatible, Self'''


if __name__ == "__main__":
Expand Down

0 comments on commit 3d1e881

Please sign in to comment.