Skip to content

Commit

Permalink
Fix type annotations for get_axis_num (GH 9822) (#9827)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bmerry authored Nov 26, 2024
1 parent e0ec77e commit f3a65d5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Deprecations

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

Expand Down
3 changes: 3 additions & 0 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ def __iter__(self: Any) -> Iterator[Any]:
raise TypeError("iteration over a 0-d array")
return self._iter()

@overload
def get_axis_num(self, dim: str) -> int: ... # type: ignore [overload-overlap]

@overload
def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ...

Expand Down
3 changes: 3 additions & 0 deletions xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,9 @@ def _dask_finalize(
data = array_func(results, *args, **kwargs)
return type(self)(self._dims, data, attrs=self._attrs)

@overload
def get_axis_num(self, dim: str) -> int: ... # type: ignore [overload-overlap]

@overload
def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ...

Expand Down

0 comments on commit f3a65d5

Please sign in to comment.