Skip to content

Commit

Permalink
type hint improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Jan 5, 2022
1 parent f966938 commit 29e312f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import copy
from itertools import chain, zip_longest
import operator
from typing import Sequence, Union
from typing import Iterable, Optional, Union
import warnings
import zlib

Expand All @@ -39,6 +39,11 @@
import iris.time
import iris.util

# Define some typing aliases.
Dims = Union[int, Iterable[int]]
RealData = Union[np.ndarray, ma.MaskedArray]
RealOrLazyData = Union[RealData, da.Array]


class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta):
"""
Expand Down Expand Up @@ -250,7 +255,7 @@ def _lazy_values(self):
"""
return self._values_dm.lazy_data()

def _core_values(self) -> Union[npt.NDArray, "da.Array"]:
def _core_values(self) -> RealOrLazyData:
"""
The values array of this dimensional metadata which may be a NumPy
array or a dask array.
Expand Down Expand Up @@ -1447,7 +1452,7 @@ def points(self, points):
self._values = points

@property
def bounds(self) -> npt.NDArray:
def bounds(self) -> RealData:
"""
The coordinate bounds values, as a NumPy array,
or None if no bound values are defined.
Expand Down Expand Up @@ -1565,15 +1570,15 @@ def lazy_bounds(self):
lazy_bounds = self._bounds_dm.lazy_data()
return lazy_bounds

def core_points(self):
def core_points(self) -> RealOrLazyData:
"""
The points array at the core of this coord, which may be a NumPy array
or a dask array.
"""
return super()._core_values()

def core_bounds(self) -> Union[npt.NDArray, "da.Array"]:
def core_bounds(self) -> RealOrLazyData:
"""
The points array at the core of this coord, which may be a NumPy array
or a dask array.
Expand Down Expand Up @@ -1935,9 +1940,7 @@ def cell(self, index):

return Cell(point, bound)

def collapsed(
self, dims_to_collapse: Union[int, Sequence[int], None] = None
) -> "Coord":
def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord":
"""
Returns a copy of this coordinate, which has been collapsed along
the specified dimensions.
Expand All @@ -1956,8 +1959,8 @@ def collapsed(
# Collapse the coordinate by serializing the points and
# bounds as strings.
def serialize(
x: npt.NDArray, axis: Union[Sequence[int], None]
) -> Union[npt.NDArray, str]:
x: npt.NDArray[np.str_], axis: Optional[Iterable[int]]
) -> Union[npt.NDArray[np.str_], str]:
if axis is None:
return "|".join(str(i) for i in x.flatten())

Expand Down

0 comments on commit 29e312f

Please sign in to comment.