Skip to content

Commit f3a65d5

Browse files
authored
Fix type annotations for get_axis_num (GH 9822) (#9827)
* Fix type annotations for `get_axis_num` (GH 9822) Explicitly annotate that a single `str` argument leads to an `int` return, overriding the match against Iterable. * Suppress mypy errors related to get_axis_num * Add PR number to whats-new.rst
1 parent e0ec77e commit f3a65d5

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Deprecations
3636

3737
Bug fixes
3838
~~~~~~~~~
39+
- Fix type annotations for ``get_axis_num``. (:issue:`9822`, :pull:`9827`).
40+
By `Bruce Merry <https://github.com/bmerry>`_.
3941
- Fix unintended load on datasets when calling :py:meth:`DataArray.plot.scatter` (:pull:`9818`).
4042
By `Jimmy Westling <https://github.com/illviljan>`_.
4143

xarray/core/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ def __iter__(self: Any) -> Iterator[Any]:
213213
raise TypeError("iteration over a 0-d array")
214214
return self._iter()
215215

216+
@overload
217+
def get_axis_num(self, dim: str) -> int: ... # type: ignore [overload-overlap]
218+
216219
@overload
217220
def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ...
218221

xarray/namedarray/core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,9 @@ def _dask_finalize(
669669
data = array_func(results, *args, **kwargs)
670670
return type(self)(self._dims, data, attrs=self._attrs)
671671

672+
@overload
673+
def get_axis_num(self, dim: str) -> int: ... # type: ignore [overload-overlap]
674+
672675
@overload
673676
def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ...
674677

0 commit comments

Comments
 (0)