Skip to content

Commit

Permalink
Dims can be a single Hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky committed Jan 12, 2024
1 parent 08c8f9a commit f420a45
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
5 changes: 2 additions & 3 deletions xarray/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import datetime
import sys
from collections.abc import Hashable, Iterable, Iterator, Mapping, Sequence
from collections.abc import Hashable, Iterator, Mapping, Sequence
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -182,8 +182,7 @@ def copy(
DsCompatible = Union["Dataset", "DaCompatible"]
GroupByCompatible = Union["Dataset", "DataArray"]

Dims = Union[str, Iterable[Hashable], "ellipsis", None]
OrderedDims = Union[str, Sequence[Union[Hashable, "ellipsis"]], "ellipsis", None]
Dims = Union[Hashable, "ellipsis", Sequence[Union[Hashable, "ellipsis"]], None]

# FYI in some cases we don't allow `None`, which this doesn't take account of.
T_ChunkDim: TypeAlias = Union[int, Literal["auto"], None, tuple[int, ...]]
Expand Down
16 changes: 7 additions & 9 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
import pandas as pd

if TYPE_CHECKING:
from xarray.core.types import Dims, ErrorOptionsWithWarn, OrderedDims, T_DuckArray
from xarray.core.types import Dims, ErrorOptionsWithWarn, T_DuckArray

K = TypeVar("K")
V = TypeVar("V")
Expand Down Expand Up @@ -1051,7 +1051,7 @@ def parse_dims(

@overload
def parse_ordered_dims(
dim: str | Sequence[Hashable | ellipsis] | T_None,
dim: Dims,
all_dims: tuple[Hashable, ...],
*,
check_exists: bool = True,
Expand All @@ -1062,7 +1062,7 @@ def parse_ordered_dims(

@overload
def parse_ordered_dims(
dim: str | Sequence[Hashable | ellipsis] | T_None,
dim: Dims,
all_dims: tuple[Hashable, ...],
*,
check_exists: bool = True,
Expand All @@ -1072,24 +1072,22 @@ def parse_ordered_dims(


def parse_ordered_dims(
dim: OrderedDims,
dim: Dims,
all_dims: tuple[Hashable, ...],
*,
check_exists: bool = True,
replace_none: bool = True,
) -> tuple[Hashable, ...] | None | ellipsis:
"""Parse one or more dimensions.
A single dimension must be always a str, multiple dimensions
can be Hashables. This supports e.g. using a tuple as a dimension.
An ellipsis ("...") in a sequence of dimensions will be
replaced with all remaining dimensions. This only makes sense when
the input is a sequence and not e.g. a set.
Parameters
----------
dim : str, Sequence of Hashable or "...", "..." or None
Dimension(s) to parse. If "..." appears in a Sequence
dim : Hashable or ... (or Sequence thereof), or None
Dimension(s) to parse. If ... appears in a Sequence
it always gets replaced with all remaining dims
all_dims : tuple of Hashable
All possible dimensions.
Expand All @@ -1103,7 +1101,7 @@ def parse_ordered_dims(
parsed_dims : tuple of Hashable
Input dimensions as a tuple.
"""
if dim is not None and dim is not ... and not isinstance(dim, str) and ... in dim:
if isinstance(dim, Sequence) and not isinstance(dim, str) and ... in dim:
dims_set: set[Hashable | ellipsis] = set(dim)
all_dims_set = set(all_dims)
if check_exists:
Expand Down
9 changes: 3 additions & 6 deletions xarray/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from collections.abc import Hashable, Iterable, Sequence
from collections.abc import Hashable, Iterable

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -297,7 +297,7 @@ def test_parse_dims_replace_none(dim: None | ellipsis) -> None:
pytest.param(["x", 2], id="list_missing_all"),
],
)
def test_parse_dims_raises(dim: str | Iterable[Hashable]) -> None:
def test_parse_dims_raises(dim):
all_dims = ("a", "b", 1, ("b", "c")) # selection of different Hashables
with pytest.raises(ValueError, match="'x'"):
utils.parse_dims(dim, all_dims, check_exists=True)
Expand All @@ -313,10 +313,7 @@ def test_parse_dims_raises(dim: str | Iterable[Hashable]) -> None:
pytest.param(["a", ..., "b"], ("a", "c", "b"), id="list_with_middle_ellipsis"),
],
)
def test_parse_ordered_dims(
dim: str | Sequence[Hashable | ellipsis],
expected: tuple[Hashable, ...],
) -> None:
def test_parse_ordered_dims(dim, expected):
all_dims = ("a", "b", "c")
actual = utils.parse_ordered_dims(dim, all_dims)
assert actual == expected
Expand Down

0 comments on commit f420a45

Please sign in to comment.