Skip to content

Commit 90aa51d

Browse files
Use EllipsisType (#9418)
* Upgrade mypy to 1.11 This does a few things: - Upgrades mypy - Adds lots of ignores for mpl & pandas type issues in tests -- not as good as fixing them, but better than staying on mypy 1.8 - Removes some old ignores - Adds a couple of ignores with notes where it's our issue and I'm not sure what's going on - Starts using the `EllipsisType` in a couple of places Note that I used a tool to mass-add ignores, I didn't add them all manually * Use `EllipsisType` Now with python3.10 we can roll this out. Stacks on #9417, merge that first * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * . --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d4153f1 commit 90aa51d

File tree

8 files changed

+19
-15
lines changed

8 files changed

+19
-15
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ reportMissingTypeStubs = false
236236
# ]
237237

238238
[tool.ruff]
239-
builtins = ["ellipsis"]
240239
extend-exclude = [
241240
"doc",
242241
"_typed_ops.pyi",
@@ -254,7 +253,7 @@ ignore = [
254253
"E402",
255254
"E501",
256255
"E731",
257-
"UP007"
256+
"UP007",
258257
]
259258
select = [
260259
"F", # Pyflakes

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5305,7 +5305,7 @@ def _get_stack_index(
53055305

53065306
def _stack_once(
53075307
self,
5308-
dims: Sequence[Hashable | ellipsis],
5308+
dims: Sequence[Hashable | EllipsisType],
53095309
new_dim: Hashable,
53105310
index_cls: type[Index],
53115311
create_index: bool | None = True,

xarray/core/types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
import sys
55
from collections.abc import Callable, Collection, Hashable, Iterator, Mapping, Sequence
6+
from types import EllipsisType
67
from typing import (
78
TYPE_CHECKING,
89
Any,
@@ -48,7 +49,7 @@
4849
try:
4950
from dask.array import Array as DaskArray
5051
except ImportError:
51-
DaskArray = np.ndarray
52+
DaskArray = np.ndarray # type: ignore[misc, assignment, unused-ignore]
5253

5354
try:
5455
from cubed import Array as CubedArray
@@ -188,7 +189,7 @@ def copy(
188189

189190
# Don't change to Hashable | Collection[Hashable]
190191
# Read: https://github.com/pydata/xarray/issues/6142
191-
Dims = Union[str, Collection[Hashable], "ellipsis", None]
192+
Dims = Union[str, Collection[Hashable], EllipsisType, None]
192193

193194
# FYI in some cases we don't allow `None`, which this doesn't take account of.
194195
# FYI the `str` is for a size string, e.g. "16MB", supported by dask.

xarray/core/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
)
6464
from enum import Enum
6565
from pathlib import Path
66+
from types import EllipsisType
6667
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeGuard, TypeVar, overload
6768

6869
import numpy as np
@@ -839,7 +840,7 @@ def parse_dims(
839840
*,
840841
check_exists: bool = True,
841842
replace_none: Literal[False],
842-
) -> tuple[Hashable, ...] | None | ellipsis: ...
843+
) -> tuple[Hashable, ...] | None | EllipsisType: ...
843844

844845

845846
def parse_dims(
@@ -848,7 +849,7 @@ def parse_dims(
848849
*,
849850
check_exists: bool = True,
850851
replace_none: bool = True,
851-
) -> tuple[Hashable, ...] | None | ellipsis:
852+
) -> tuple[Hashable, ...] | None | EllipsisType:
852853
"""Parse one or more dimensions.
853854
854855
A single dimension must be always a str, multiple dimensions
@@ -900,7 +901,7 @@ def parse_ordered_dims(
900901
*,
901902
check_exists: bool = True,
902903
replace_none: Literal[False],
903-
) -> tuple[Hashable, ...] | None | ellipsis: ...
904+
) -> tuple[Hashable, ...] | None | EllipsisType: ...
904905

905906

906907
def parse_ordered_dims(
@@ -909,7 +910,7 @@ def parse_ordered_dims(
909910
*,
910911
check_exists: bool = True,
911912
replace_none: bool = True,
912-
) -> tuple[Hashable, ...] | None | ellipsis:
913+
) -> tuple[Hashable, ...] | None | EllipsisType:
913914
"""Parse one or more dimensions.
914915
915916
A single dimension must be always a str, multiple dimensions
@@ -936,7 +937,7 @@ def parse_ordered_dims(
936937
Input dimensions as a tuple.
937938
"""
938939
if dim is not None and dim is not ... and not isinstance(dim, str) and ... in dim:
939-
dims_set: set[Hashable | ellipsis] = set(dim)
940+
dims_set: set[Hashable | EllipsisType] = set(dim)
940941
all_dims_set = set(all_dims)
941942
if check_exists:
942943
_check_dims(dims_set, all_dims_set)

xarray/core/variable.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections.abc import Callable, Hashable, Mapping, Sequence
99
from datetime import timedelta
1010
from functools import partial
11+
from types import EllipsisType
1112
from typing import TYPE_CHECKING, Any, NoReturn, cast
1213

1314
import numpy as np
@@ -1287,7 +1288,7 @@ def roll(self, shifts=None, **shifts_kwargs):
12871288
@deprecate_dims
12881289
def transpose(
12891290
self,
1290-
*dim: Hashable | ellipsis,
1291+
*dim: Hashable | EllipsisType,
12911292
missing_dims: ErrorOptionsWithWarn = "raise",
12921293
) -> Self:
12931294
"""Return a new Variable object with transposed dimensions.

xarray/namedarray/_typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
55
from enum import Enum
6-
from types import ModuleType
6+
from types import EllipsisType, ModuleType
77
from typing import (
88
TYPE_CHECKING,
99
Any,
@@ -91,7 +91,7 @@ def dtype(self) -> _DType_co: ...
9191
# TODO: np.array_api was bugged and didn't allow (None,), but should!
9292
# https://github.com/numpy/numpy/pull/25022
9393
# https://github.com/data-apis/array-api/pull/674
94-
_IndexKey = Union[int, slice, "ellipsis"]
94+
_IndexKey = Union[int, slice, EllipsisType]
9595
_IndexKeys = tuple[_IndexKey, ...] # tuple[Union[_IndexKey, None], ...]
9696
_IndexKeyLike = Union[_IndexKey, _IndexKeys]
9797

xarray/namedarray/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import warnings
77
from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
8+
from types import EllipsisType
89
from typing import (
910
TYPE_CHECKING,
1011
Any,
@@ -996,7 +997,7 @@ def _to_dense(self) -> NamedArray[Any, _DType_co]:
996997

997998
def permute_dims(
998999
self,
999-
*dim: Iterable[_Dim] | ellipsis,
1000+
*dim: Iterable[_Dim] | EllipsisType,
10001001
missing_dims: ErrorOptionsWithWarn = "raise",
10011002
) -> NamedArray[Any, _DType_co]:
10021003
"""Return a new object with transposed dimensions.

xarray/tests/test_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from collections.abc import Hashable
4+
from types import EllipsisType
45

56
import numpy as np
67
import pandas as pd
@@ -288,7 +289,7 @@ def test_parse_dims_set() -> None:
288289
@pytest.mark.parametrize(
289290
"dim", [pytest.param(None, id="None"), pytest.param(..., id="ellipsis")]
290291
)
291-
def test_parse_dims_replace_none(dim: None | ellipsis) -> None:
292+
def test_parse_dims_replace_none(dim: None | EllipsisType) -> None:
292293
all_dims = ("a", "b", 1, ("b", "c")) # selection of different Hashables
293294
actual = utils.parse_dims(dim, all_dims, replace_none=True)
294295
assert actual == all_dims

0 commit comments

Comments
 (0)