Skip to content

Commit 9d1aa93

Browse files
committed
type hint improvements
1 parent 08565df commit 9d1aa93

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/iris/coords.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from functools import lru_cache
1616
from itertools import zip_longest
1717
import operator
18-
from typing import Sequence, Union
18+
from typing import Iterable, Optional, Union
1919
import warnings
2020
import zlib
2121

@@ -39,6 +39,11 @@
3939
import iris.time
4040
import iris.util
4141

42+
# Define some typing aliases.
43+
Dims = Union[int, Iterable[int]]
44+
RealData = Union[np.ndarray, ma.MaskedArray]
45+
RealOrLazyData = Union[RealData, da.Array]
46+
4247

4348
class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta):
4449
"""
@@ -254,7 +259,7 @@ def _lazy_values(self):
254259
"""
255260
return self._values_dm.lazy_data()
256261

257-
def _core_values(self) -> Union[npt.NDArray, "da.Array"]:
262+
def _core_values(self) -> RealOrLazyData:
258263
"""
259264
The values array of this dimensional metadata which may be a NumPy
260265
array or a dask array.
@@ -1659,7 +1664,7 @@ def points(self, points):
16591664
self._values = points
16601665

16611666
@property
1662-
def bounds(self) -> npt.NDArray:
1667+
def bounds(self) -> RealData:
16631668
"""
16641669
The coordinate bounds values, as a NumPy array,
16651670
or None if no bound values are defined.
@@ -1777,15 +1782,15 @@ def lazy_bounds(self):
17771782
lazy_bounds = self._bounds_dm.lazy_data()
17781783
return lazy_bounds
17791784

1780-
def core_points(self):
1785+
def core_points(self) -> RealOrLazyData:
17811786
"""
17821787
The points array at the core of this coord, which may be a NumPy array
17831788
or a dask array.
17841789
17851790
"""
17861791
return super()._core_values()
17871792

1788-
def core_bounds(self) -> Union[npt.NDArray, "da.Array"]:
1793+
def core_bounds(self) -> RealOrLazyData:
17891794
"""
17901795
The points array at the core of this coord, which may be a NumPy array
17911796
or a dask array.
@@ -2184,9 +2189,7 @@ def cell(self, index):
21842189

21852190
return Cell(point, bound)
21862191

2187-
def collapsed(
2188-
self, dims_to_collapse: Union[int, Sequence[int], None] = None
2189-
) -> "Coord":
2192+
def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord":
21902193
"""
21912194
Returns a copy of this coordinate, which has been collapsed along
21922195
the specified dimensions.
@@ -2205,8 +2208,8 @@ def collapsed(
22052208
# Collapse the coordinate by serializing the points and
22062209
# bounds as strings.
22072210
def serialize(
2208-
x: npt.NDArray, axis: Union[Sequence[int], None]
2209-
) -> Union[npt.NDArray, str]:
2211+
x: npt.NDArray[np.str_], axis: Optional[Iterable[int]]
2212+
) -> Union[npt.NDArray[np.str_], str]:
22102213
if axis is None:
22112214
return "|".join(str(i) for i in x.flatten())
22122215

0 commit comments

Comments
 (0)