Skip to content

Commit 0462a2c

Browse files
committed
type hint improvements
1 parent c503da0 commit 0462a2c

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 chain, 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.
@@ -1677,7 +1682,7 @@ def points(self, points):
16771682
self._values = points
16781683

16791684
@property
1680-
def bounds(self) -> npt.NDArray:
1685+
def bounds(self) -> RealData:
16811686
"""
16821687
The coordinate bounds values, as a NumPy array,
16831688
or None if no bound values are defined.
@@ -1795,15 +1800,15 @@ def lazy_bounds(self):
17951800
lazy_bounds = self._bounds_dm.lazy_data()
17961801
return lazy_bounds
17971802

1798-
def core_points(self):
1803+
def core_points(self) -> RealOrLazyData:
17991804
"""
18001805
The points array at the core of this coord, which may be a NumPy array
18011806
or a dask array.
18021807
18031808
"""
18041809
return super()._core_values()
18051810

1806-
def core_bounds(self) -> Union[npt.NDArray, "da.Array"]:
1811+
def core_bounds(self) -> RealOrLazyData:
18071812
"""
18081813
The points array at the core of this coord, which may be a NumPy array
18091814
or a dask array.
@@ -2172,9 +2177,7 @@ def cell(self, index):
21722177

21732178
return Cell(point, bound)
21742179

2175-
def collapsed(
2176-
self, dims_to_collapse: Union[int, Sequence[int], None] = None
2177-
) -> "Coord":
2180+
def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord":
21782181
"""
21792182
Returns a copy of this coordinate, which has been collapsed along
21802183
the specified dimensions.
@@ -2193,8 +2196,8 @@ def collapsed(
21932196
# Collapse the coordinate by serializing the points and
21942197
# bounds as strings.
21952198
def serialize(
2196-
x: npt.NDArray, axis: Union[Sequence[int], None]
2197-
) -> Union[npt.NDArray, str]:
2199+
x: npt.NDArray[np.str_], axis: Optional[Iterable[int]]
2200+
) -> Union[npt.NDArray[np.str_], str]:
21982201
if axis is None:
21992202
return "|".join(str(i) for i in x.flatten())
22002203

0 commit comments

Comments
 (0)