Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EllipsisType #9418

Merged
merged 9 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ reportMissingTypeStubs = false
# ]

[tool.ruff]
builtins = ["ellipsis"]
extend-exclude = [
"doc",
"_typed_ops.pyi",
Expand All @@ -254,7 +253,7 @@ ignore = [
"E402",
"E501",
"E731",
"UP007"
"UP007",
]
select = [
"F", # Pyflakes
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5305,7 +5305,7 @@ def _get_stack_index(

def _stack_once(
self,
dims: Sequence[Hashable | ellipsis],
dims: Sequence[Hashable | EllipsisType],
new_dim: Hashable,
index_cls: type[Index],
create_index: bool | None = True,
Expand Down
5 changes: 3 additions & 2 deletions xarray/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import sys
from collections.abc import Callable, Collection, Hashable, Iterator, Mapping, Sequence
from types import EllipsisType
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -48,7 +49,7 @@
try:
from dask.array import Array as DaskArray
except ImportError:
DaskArray = np.ndarray
DaskArray = np.ndarray # type: ignore[misc, assignment, unused-ignore]

try:
from cubed import Array as CubedArray
Expand Down Expand Up @@ -188,7 +189,7 @@ def copy(

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

# FYI in some cases we don't allow `None`, which this doesn't take account of.
# FYI the `str` is for a size string, e.g. "16MB", supported by dask.
Expand Down
11 changes: 6 additions & 5 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
)
from enum import Enum
from pathlib import Path
from types import EllipsisType
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeGuard, TypeVar, overload

import numpy as np
Expand Down Expand Up @@ -839,7 +840,7 @@ def parse_dims(
*,
check_exists: bool = True,
replace_none: Literal[False],
) -> tuple[Hashable, ...] | None | ellipsis: ...
) -> tuple[Hashable, ...] | None | EllipsisType: ...


def parse_dims(
Expand All @@ -848,7 +849,7 @@ def parse_dims(
*,
check_exists: bool = True,
replace_none: bool = True,
) -> tuple[Hashable, ...] | None | ellipsis:
) -> tuple[Hashable, ...] | None | EllipsisType:
"""Parse one or more dimensions.

A single dimension must be always a str, multiple dimensions
Expand Down Expand Up @@ -900,7 +901,7 @@ def parse_ordered_dims(
*,
check_exists: bool = True,
replace_none: Literal[False],
) -> tuple[Hashable, ...] | None | ellipsis: ...
) -> tuple[Hashable, ...] | None | EllipsisType: ...


def parse_ordered_dims(
Expand All @@ -909,7 +910,7 @@ def parse_ordered_dims(
*,
check_exists: bool = True,
replace_none: bool = True,
) -> tuple[Hashable, ...] | None | ellipsis:
) -> tuple[Hashable, ...] | None | EllipsisType:
"""Parse one or more dimensions.

A single dimension must be always a str, multiple dimensions
Expand All @@ -936,7 +937,7 @@ def parse_ordered_dims(
Input dimensions as a tuple.
"""
if dim is not None and dim is not ... and not isinstance(dim, str) and ... in dim:
dims_set: set[Hashable | ellipsis] = set(dim)
dims_set: set[Hashable | EllipsisType] = set(dim)
all_dims_set = set(all_dims)
if check_exists:
_check_dims(dims_set, all_dims_set)
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections.abc import Callable, Hashable, Mapping, Sequence
from datetime import timedelta
from functools import partial
from types import EllipsisType
from typing import TYPE_CHECKING, Any, NoReturn, cast

import numpy as np
Expand Down Expand Up @@ -1287,7 +1288,7 @@ def roll(self, shifts=None, **shifts_kwargs):
@deprecate_dims
def transpose(
self,
*dim: Hashable | ellipsis,
*dim: Hashable | EllipsisType,
missing_dims: ErrorOptionsWithWarn = "raise",
) -> Self:
"""Return a new Variable object with transposed dimensions.
Expand Down
4 changes: 2 additions & 2 deletions xarray/namedarray/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
from enum import Enum
from types import ModuleType
from types import EllipsisType, ModuleType
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -91,7 +91,7 @@ def dtype(self) -> _DType_co: ...
# TODO: np.array_api was bugged and didn't allow (None,), but should!
# https://github.com/numpy/numpy/pull/25022
# https://github.com/data-apis/array-api/pull/674
_IndexKey = Union[int, slice, "ellipsis"]
_IndexKey = Union[int, slice, EllipsisType]
_IndexKeys = tuple[_IndexKey, ...] # tuple[Union[_IndexKey, None], ...]
_IndexKeyLike = Union[_IndexKey, _IndexKeys]

Expand Down
3 changes: 2 additions & 1 deletion xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import warnings
from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
from types import EllipsisType
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -996,7 +997,7 @@ def _to_dense(self) -> NamedArray[Any, _DType_co]:

def permute_dims(
self,
*dim: Iterable[_Dim] | ellipsis,
*dim: Iterable[_Dim] | EllipsisType,
missing_dims: ErrorOptionsWithWarn = "raise",
) -> NamedArray[Any, _DType_co]:
"""Return a new object with transposed dimensions.
Expand Down
3 changes: 2 additions & 1 deletion xarray/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from collections.abc import Hashable
from types import EllipsisType

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -288,7 +289,7 @@ def test_parse_dims_set() -> None:
@pytest.mark.parametrize(
"dim", [pytest.param(None, id="None"), pytest.param(..., id="ellipsis")]
)
def test_parse_dims_replace_none(dim: None | ellipsis) -> None:
def test_parse_dims_replace_none(dim: None | EllipsisType) -> None:
all_dims = ("a", "b", 1, ("b", "c")) # selection of different Hashables
actual = utils.parse_dims(dim, all_dims, replace_none=True)
assert actual == all_dims
Expand Down
Loading