From c2080ee8deb4d92ab4b9cfa9907db5d9bbca3eb6 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Fri, 19 Nov 2021 16:49:25 +0000 Subject: [PATCH 1/5] add type hints --- lib/iris/coords.py | 69 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 8afe9dad41..774ed2744b 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -11,12 +11,14 @@ from functools import lru_cache from itertools import zip_longest import operator +from typing import Sequence, Union import warnings import zlib import dask.array as da import numpy as np import numpy.ma as ma +import numpy.typing as npt from iris._data_manager import DataManager import iris._lazy_data as _lazy @@ -238,8 +240,24 @@ def _lazy_values(self): """Return a lazy array representing the dimensional metadata values.""" return self._values_dm.lazy_data() +<<<<<<< HEAD def _core_values(self): """Value array of this dimensional metadata which may be a NumPy array or a dask array.""" +||||||| constructed merge base + def _core_values(self): + """ + The values array of this dimensional metadata which may be a NumPy + array or a dask array. + + """ +======= + def _core_values(self) -> Union[npt.NDArray, "da.Array"]: + """ + The values array of this dimensional metadata which may be a NumPy + array or a dask array. + + """ +>>>>>>> add type hints result = self._values_dm.core_data() if not _lazy.is_lazy_data(result): result = result.view() @@ -771,8 +789,24 @@ def dtype(self): return self._values_dm.dtype @property +<<<<<<< HEAD def ndim(self): """Return the number of dimensions of the current dimensional metadata object.""" +||||||| constructed merge base + def ndim(self): + """ + Return the number of dimensions of the current dimensional metadata + object. + + """ +======= + def ndim(self) -> int: + """ + Return the number of dimensions of the current dimensional metadata + object. + + """ +>>>>>>> add type hints return self._values_dm.ndim def has_bounds(self): @@ -1585,9 +1619,17 @@ def points(self, points): self._values = points @property +<<<<<<< HEAD def bounds(self): """Coordinate bounds values. +||||||| constructed merge base + def bounds(self): + """ +======= + def bounds(self) -> npt.NDArray: + """ +>>>>>>> add type hints The coordinate bounds values, as a NumPy array, or None if no bound values are defined. @@ -1721,8 +1763,24 @@ def core_points(self): """Core points array at the core of this coord, which may be a NumPy array or a dask array.""" return super()._core_values() +<<<<<<< HEAD def core_bounds(self): """Core bounds. The points array at the core of this coord, which may be a NumPy array or a dask array.""" +||||||| constructed merge base + def core_bounds(self): + """ + The points array at the core of this coord, which may be a NumPy array + or a dask array. + + """ +======= + def core_bounds(self) -> Union[npt.NDArray, "da.Array"]: + """ + The points array at the core of this coord, which may be a NumPy array + or a dask array. + + """ +>>>>>>> add type hints result = None if self.has_bounds(): result = self._bounds_dm.core_data() @@ -2100,8 +2158,11 @@ def cell(self, index): return Cell(point, bound) - def collapsed(self, dims_to_collapse=None): - """Return a copy of this coordinate, which has been collapsed along the specified dimensions. + def collapsed( + self, dims_to_collapse: Union[int, Sequence[int], None] = None + ) -> "Coord": + """ + Returns a copy of this coordinate, which has been collapsed along the specified dimensions. Replaces the points & bounds with a simple bounded region. """ @@ -2115,7 +2176,9 @@ def collapsed(self, dims_to_collapse=None): if np.issubdtype(self.dtype, np.str_): # Collapse the coordinate by serializing the points and # bounds as strings. - def serialize(x, axis): + def serialize( + x: npt.NDArray, axis: Union[Sequence[int], None] + ) -> Union[npt.NDArray, str]: if axis is None: return "|".join(str(i) for i in x.flatten()) From 470bb0cb2576a486693be7c7f4653d241f87f012 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Wed, 5 Jan 2022 13:24:33 +0000 Subject: [PATCH 2/5] type hint improvements --- lib/iris/coords.py | 82 ++++++++-------------------------------------- 1 file changed, 14 insertions(+), 68 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 774ed2744b..b3c3d1473b 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -11,7 +11,7 @@ from functools import lru_cache from itertools import zip_longest import operator -from typing import Sequence, Union +from typing import Iterable, Optional, Union import warnings import zlib @@ -34,11 +34,15 @@ import iris.exceptions import iris.time import iris.util -import iris.warnings #: The default value for ignore_axis which controls guess_coord_axis' behaviour DEFAULT_IGNORE_AXIS = False +# Define some typing aliases. +Dims = Union[int, Iterable[int]] +RealData = Union[np.ndarray, ma.MaskedArray] +RealOrLazyData = Union[RealData, da.Array] + class _DimensionalMetadata(CFVariableMixin, metaclass=ABCMeta): """Superclass for dimensional metadata.""" @@ -240,24 +244,8 @@ def _lazy_values(self): """Return a lazy array representing the dimensional metadata values.""" return self._values_dm.lazy_data() -<<<<<<< HEAD - def _core_values(self): + def _core_values(self) -> RealOrLazyData: """Value array of this dimensional metadata which may be a NumPy array or a dask array.""" -||||||| constructed merge base - def _core_values(self): - """ - The values array of this dimensional metadata which may be a NumPy - array or a dask array. - - """ -======= - def _core_values(self) -> Union[npt.NDArray, "da.Array"]: - """ - The values array of this dimensional metadata which may be a NumPy - array or a dask array. - - """ ->>>>>>> add type hints result = self._values_dm.core_data() if not _lazy.is_lazy_data(result): result = result.view() @@ -789,24 +777,8 @@ def dtype(self): return self._values_dm.dtype @property -<<<<<<< HEAD - def ndim(self): - """Return the number of dimensions of the current dimensional metadata object.""" -||||||| constructed merge base - def ndim(self): - """ - Return the number of dimensions of the current dimensional metadata - object. - - """ -======= def ndim(self) -> int: - """ - Return the number of dimensions of the current dimensional metadata - object. - - """ ->>>>>>> add type hints + """Return the number of dimensions of the current dimensional metadata object.""" return self._values_dm.ndim def has_bounds(self): @@ -1619,17 +1591,9 @@ def points(self, points): self._values = points @property -<<<<<<< HEAD - def bounds(self): + def bounds(self) -> RealData: """Coordinate bounds values. -||||||| constructed merge base - def bounds(self): - """ -======= - def bounds(self) -> npt.NDArray: - """ ->>>>>>> add type hints The coordinate bounds values, as a NumPy array, or None if no bound values are defined. @@ -1759,28 +1723,12 @@ def lazy_bounds(self): lazy_bounds = self._bounds_dm.lazy_data() return lazy_bounds - def core_points(self): + def core_points(self) -> RealOrLazyData: """Core points array at the core of this coord, which may be a NumPy array or a dask array.""" return super()._core_values() -<<<<<<< HEAD - def core_bounds(self): + def core_bounds(self) -> RealOrLazyData: """Core bounds. The points array at the core of this coord, which may be a NumPy array or a dask array.""" -||||||| constructed merge base - def core_bounds(self): - """ - The points array at the core of this coord, which may be a NumPy array - or a dask array. - - """ -======= - def core_bounds(self) -> Union[npt.NDArray, "da.Array"]: - """ - The points array at the core of this coord, which may be a NumPy array - or a dask array. - - """ ->>>>>>> add type hints result = None if self.has_bounds(): result = self._bounds_dm.core_data() @@ -2158,9 +2106,7 @@ def cell(self, index): return Cell(point, bound) - def collapsed( - self, dims_to_collapse: Union[int, Sequence[int], None] = None - ) -> "Coord": + def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord": """ Returns a copy of this coordinate, which has been collapsed along the specified dimensions. @@ -2177,8 +2123,8 @@ def collapsed( # Collapse the coordinate by serializing the points and # bounds as strings. def serialize( - x: npt.NDArray, axis: Union[Sequence[int], None] - ) -> Union[npt.NDArray, str]: + x: npt.NDArray[np.str_], axis: Optional[Iterable[int]] + ) -> Union[npt.NDArray[np.str_], str]: if axis is None: return "|".join(str(i) for i in x.flatten()) From 3e4cb4dd560910d5f241d4e876676b6108d00e91 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Wed, 5 Jan 2022 14:31:27 +0000 Subject: [PATCH 3/5] dims_to_collapse always tuple --- lib/iris/coords.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index b3c3d1473b..2d30b418c1 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2116,7 +2116,7 @@ def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord": # through to numpy if isinstance(dims_to_collapse, (int, np.integer)): dims_to_collapse = (dims_to_collapse,) - if isinstance(dims_to_collapse, list): + if isinstance(dims_to_collapse, Iterable): dims_to_collapse = tuple(dims_to_collapse) if np.issubdtype(self.dtype, np.str_): From d6e7f1b36e56409440646be892ca6837ad4edd27 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Thu, 6 Jan 2022 17:15:50 +0000 Subject: [PATCH 4/5] bounds can be None --- lib/iris/coords.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 2d30b418c1..c4c4daac88 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -1591,7 +1591,7 @@ def points(self, points): self._values = points @property - def bounds(self) -> RealData: + def bounds(self) -> Optional[RealData]: """Coordinate bounds values. The coordinate bounds values, as a NumPy array, @@ -1727,7 +1727,7 @@ def core_points(self) -> RealOrLazyData: """Core points array at the core of this coord, which may be a NumPy array or a dask array.""" return super()._core_values() - def core_bounds(self) -> RealOrLazyData: + def core_bounds(self) -> Optional[RealOrLazyData]: """Core bounds. The points array at the core of this coord, which may be a NumPy array or a dask array.""" result = None if self.has_bounds(): From 1a63e4c22e6f6fa413ed91deab57a4df1805e0bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 30 Aug 2024 14:34:50 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lib/iris/coords.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index c4c4daac88..97a50e5608 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2107,8 +2107,7 @@ def cell(self, index): return Cell(point, bound) def collapsed(self, dims_to_collapse: Optional[Dims] = None) -> "Coord": - """ - Returns a copy of this coordinate, which has been collapsed along the specified dimensions. + """Returns a copy of this coordinate, which has been collapsed along the specified dimensions. Replaces the points & bounds with a simple bounded region. """