From 1f187b1d6bb40cb1689692370a56d54893be38ae Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Tue, 26 Nov 2024 08:10:24 +0200 Subject: [PATCH 1/3] 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. --- doc/whats-new.rst | 3 ++- xarray/core/common.py | 3 +++ xarray/namedarray/core.py | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 906fd0a25b2..c1e7b336d1f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -36,7 +36,8 @@ Deprecations Bug fixes ~~~~~~~~~ - +- Fix type annotations for ``get_axis_num``. (:issue:`9822`). + By `Bruce Merry `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/common.py b/xarray/core/common.py index 32135996d3c..12d30383df2 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -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: ... + @overload def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ... diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index 98d96c73e91..098f5d27122 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -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: ... + @overload def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ... From 02dad8f13aa06f138124f91db5bba8a96f0d2df3 Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Tue, 26 Nov 2024 08:42:51 +0200 Subject: [PATCH 2/3] Suppress mypy errors related to get_axis_num --- xarray/core/common.py | 2 +- xarray/namedarray/core.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 12d30383df2..3a70c9ec585 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -214,7 +214,7 @@ def __iter__(self: Any) -> Iterator[Any]: return self._iter() @overload - def get_axis_num(self, dim: str) -> int: ... + def get_axis_num(self, dim: str) -> int: ... # type: ignore [overload-overlap] @overload def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ... diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index 098f5d27122..683a1266472 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -670,7 +670,7 @@ def _dask_finalize( return type(self)(self._dims, data, attrs=self._attrs) @overload - def get_axis_num(self, dim: str) -> int: ... + def get_axis_num(self, dim: str) -> int: ... # type: ignore [overload-overlap] @overload def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ... From 0f9a2f8f6576034576aab5a058067a6c0e0062e3 Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Tue, 26 Nov 2024 09:12:35 +0200 Subject: [PATCH 3/3] Add PR number to whats-new.rst --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 9a30f6ca4d8..8bd57339180 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -36,7 +36,7 @@ Deprecations Bug fixes ~~~~~~~~~ -- Fix type annotations for ``get_axis_num``. (:issue:`9822`). +- Fix type annotations for ``get_axis_num``. (:issue:`9822`, :pull:`9827`). By `Bruce Merry `_. - Fix unintended load on datasets when calling :py:meth:`DataArray.plot.scatter` (:pull:`9818`). By `Jimmy Westling `_. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy