Skip to content

Commit 39cc565

Browse files
committed
type hint improvements
1 parent 882b5be commit 39cc565

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
@@ -14,7 +14,7 @@
1414
from functools import lru_cache
1515
from itertools import zip_longest
1616
import operator
17-
from typing import Sequence, Union
17+
from typing import Iterable, Optional, Union
1818
import warnings
1919
import zlib
2020

@@ -41,6 +41,11 @@
4141
#: The default value for ignore_axis which controls guess_coord_axis' behaviour
4242
DEFAULT_IGNORE_AXIS = False
4343

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

4550
class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta):
4651
"""
@@ -256,7 +261,7 @@ def _lazy_values(self):
256261
"""
257262
return self._values_dm.lazy_data()
258263

259-
def _core_values(self) -> Union[npt.NDArray, "da.Array"]:
264+
def _core_values(self) -> RealOrLazyData:
260265
"""
261266
The values array of this dimensional metadata which may be a NumPy
262267
array or a dask array.
@@ -1673,7 +1678,7 @@ def points(self, points):
16731678
self._values = points
16741679

16751680
@property
1676-
def bounds(self) -> npt.NDArray:
1681+
def bounds(self) -> RealData:
16771682
"""
16781683
The coordinate bounds values, as a NumPy array,
16791684
or None if no bound values are defined.
@@ -1809,15 +1814,15 @@ def lazy_bounds(self):
18091814
lazy_bounds = self._bounds_dm.lazy_data()
18101815
return lazy_bounds
18111816

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

1820-
def core_bounds(self) -> Union[npt.NDArray, "da.Array"]:
1825+
def core_bounds(self) -> RealOrLazyData:
18211826
"""
18221827
The points array at the core of this coord, which may be a NumPy array
18231828
or a dask array.
@@ -2216,9 +2221,7 @@ def cell(self, index):
22162221

22172222
return Cell(point, bound)
22182223

2219-
def collapsed(
2220-
self, dims_to_collapse: Union[int, Sequence[int], None] = None
2221-
) -> "Coord":
2224+
def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord":
22222225
"""
22232226
Returns a copy of this coordinate, which has been collapsed along
22242227
the specified dimensions.
@@ -2237,8 +2240,8 @@ def collapsed(
22372240
# Collapse the coordinate by serializing the points and
22382241
# bounds as strings.
22392242
def serialize(
2240-
x: npt.NDArray, axis: Union[Sequence[int], None]
2241-
) -> Union[npt.NDArray, str]:
2243+
x: npt.NDArray[np.str_], axis: Optional[Iterable[int]]
2244+
) -> Union[npt.NDArray[np.str_], str]:
22422245
if axis is None:
22432246
return "|".join(str(i) for i in x.flatten())
22442247

0 commit comments

Comments
 (0)