Skip to content

Commit 8f3419e

Browse files
committed
type hint improvements
1 parent 07a3e77 commit 8f3419e

File tree

1 file changed

+14
-68
lines changed

1 file changed

+14
-68
lines changed

lib/iris/coords.py

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from functools import lru_cache
1212
from itertools import zip_longest
1313
import operator
14-
from typing import Sequence, Union
14+
from typing import Iterable, Optional, Union
1515
import warnings
1616
import zlib
1717

@@ -34,11 +34,15 @@
3434
import iris.exceptions
3535
import iris.time
3636
import iris.util
37-
import iris.warnings
3837

3938
#: The default value for ignore_axis which controls guess_coord_axis' behaviour
4039
DEFAULT_IGNORE_AXIS = False
4140

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

4347
class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta):
4448
"""Superclass for dimensional metadata."""
@@ -240,24 +244,8 @@ def _lazy_values(self):
240244
"""Return a lazy array representing the dimensional metadata values."""
241245
return self._values_dm.lazy_data()
242246

243-
<<<<<<< HEAD
244-
def _core_values(self):
247+
def _core_values(self) -> RealOrLazyData:
245248
"""Value array of this dimensional metadata which may be a NumPy array or a dask array."""
246-
||||||| constructed merge base
247-
def _core_values(self):
248-
"""
249-
The values array of this dimensional metadata which may be a NumPy
250-
array or a dask array.
251-
252-
"""
253-
=======
254-
def _core_values(self) -> Union[npt.NDArray, "da.Array"]:
255-
"""
256-
The values array of this dimensional metadata which may be a NumPy
257-
array or a dask array.
258-
259-
"""
260-
>>>>>>> add type hints
261249
result = self._values_dm.core_data()
262250
if not _lazy.is_lazy_data(result):
263251
result = result.view()
@@ -789,24 +777,8 @@ def dtype(self):
789777
return self._values_dm.dtype
790778

791779
@property
792-
<<<<<<< HEAD
793-
def ndim(self):
794-
"""Return the number of dimensions of the current dimensional metadata object."""
795-
||||||| constructed merge base
796-
def ndim(self):
797-
"""
798-
Return the number of dimensions of the current dimensional metadata
799-
object.
800-
801-
"""
802-
=======
803780
def ndim(self) -> int:
804-
"""
805-
Return the number of dimensions of the current dimensional metadata
806-
object.
807-
808-
"""
809-
>>>>>>> add type hints
781+
"""Return the number of dimensions of the current dimensional metadata object."""
810782
return self._values_dm.ndim
811783

812784
def has_bounds(self):
@@ -1618,17 +1590,9 @@ def points(self, points):
16181590
self._values = points
16191591

16201592
@property
1621-
<<<<<<< HEAD
1622-
def bounds(self):
1593+
def bounds(self) -> RealData:
16231594
"""Coordinate bounds values.
16241595
1625-
||||||| constructed merge base
1626-
def bounds(self):
1627-
"""
1628-
=======
1629-
def bounds(self) -> npt.NDArray:
1630-
"""
1631-
>>>>>>> add type hints
16321596
The coordinate bounds values, as a NumPy array,
16331597
or None if no bound values are defined.
16341598
@@ -1758,28 +1722,12 @@ def lazy_bounds(self):
17581722
lazy_bounds = self._bounds_dm.lazy_data()
17591723
return lazy_bounds
17601724

1761-
def core_points(self):
1725+
def core_points(self) -> RealOrLazyData:
17621726
"""Core points array at the core of this coord, which may be a NumPy array or a dask array."""
17631727
return super()._core_values()
17641728

1765-
<<<<<<< HEAD
1766-
def core_bounds(self):
1729+
def core_bounds(self) -> RealOrLazyData:
17671730
"""Core bounds. The points array at the core of this coord, which may be a NumPy array or a dask array."""
1768-
||||||| constructed merge base
1769-
def core_bounds(self):
1770-
"""
1771-
The points array at the core of this coord, which may be a NumPy array
1772-
or a dask array.
1773-
1774-
"""
1775-
=======
1776-
def core_bounds(self) -> Union[npt.NDArray, "da.Array"]:
1777-
"""
1778-
The points array at the core of this coord, which may be a NumPy array
1779-
or a dask array.
1780-
1781-
"""
1782-
>>>>>>> add type hints
17831731
result = None
17841732
if self.has_bounds():
17851733
result = self._bounds_dm.core_data()
@@ -2157,9 +2105,7 @@ def cell(self, index):
21572105

21582106
return Cell(point, bound)
21592107

2160-
def collapsed(
2161-
self, dims_to_collapse: Union[int, Sequence[int], None] = None
2162-
) -> "Coord":
2108+
def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord":
21632109
"""
21642110
Returns a copy of this coordinate, which has been collapsed along the specified dimensions.
21652111
@@ -2176,8 +2122,8 @@ def collapsed(
21762122
# Collapse the coordinate by serializing the points and
21772123
# bounds as strings.
21782124
def serialize(
2179-
x: npt.NDArray, axis: Union[Sequence[int], None]
2180-
) -> Union[npt.NDArray, str]:
2125+
x: npt.NDArray[np.str_], axis: Optional[Iterable[int]]
2126+
) -> Union[npt.NDArray[np.str_], str]:
21812127
if axis is None:
21822128
return "|".join(str(i) for i in x.flatten())
21832129

0 commit comments

Comments
 (0)