Skip to content

Commit 252b540

Browse files
committed
type hint improvements
1 parent eb28212 commit 252b540

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

@@ -40,6 +40,11 @@
4040
import iris.time
4141
import iris.util
4242

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

4449
class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta):
4550
"""
@@ -255,7 +260,7 @@ def _lazy_values(self):
255260
"""
256261
return self._values_dm.lazy_data()
257262

258-
def _core_values(self) -> Union[npt.NDArray, "da.Array"]:
263+
def _core_values(self) -> RealOrLazyData:
259264
"""
260265
The values array of this dimensional metadata which may be a NumPy
261266
array or a dask array.
@@ -1693,7 +1698,7 @@ def points(self, points):
16931698
self._values = points
16941699

16951700
@property
1696-
def bounds(self) -> npt.NDArray:
1701+
def bounds(self) -> RealData:
16971702
"""
16981703
The coordinate bounds values, as a NumPy array,
16991704
or None if no bound values are defined.
@@ -1811,15 +1816,15 @@ def lazy_bounds(self):
18111816
lazy_bounds = self._bounds_dm.lazy_data()
18121817
return lazy_bounds
18131818

1814-
def core_points(self):
1819+
def core_points(self) -> RealOrLazyData:
18151820
"""
18161821
The points array at the core of this coord, which may be a NumPy array
18171822
or a dask array.
18181823
18191824
"""
18201825
return super()._core_values()
18211826

1822-
def core_bounds(self) -> Union[npt.NDArray, "da.Array"]:
1827+
def core_bounds(self) -> RealOrLazyData:
18231828
"""
18241829
The points array at the core of this coord, which may be a NumPy array
18251830
or a dask array.
@@ -2188,9 +2193,7 @@ def cell(self, index):
21882193

21892194
return Cell(point, bound)
21902195

2191-
def collapsed(
2192-
self, dims_to_collapse: Union[int, Sequence[int], None] = None
2193-
) -> "Coord":
2196+
def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord":
21942197
"""
21952198
Returns a copy of this coordinate, which has been collapsed along
21962199
the specified dimensions.
@@ -2209,8 +2212,8 @@ def collapsed(
22092212
# Collapse the coordinate by serializing the points and
22102213
# bounds as strings.
22112214
def serialize(
2212-
x: npt.NDArray, axis: Union[Sequence[int], None]
2213-
) -> Union[npt.NDArray, str]:
2215+
x: npt.NDArray[np.str_], axis: Optional[Iterable[int]]
2216+
) -> Union[npt.NDArray[np.str_], str]:
22142217
if axis is None:
22152218
return "|".join(str(i) for i in x.flatten())
22162219

0 commit comments

Comments
 (0)