-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support non-str Hashables in DataArray (#8559)
* support hashable dims in DataArray * add whats-new * remove uneccessary except ImportErrors * improve some typing
- Loading branch information
Showing
6 changed files
with
91 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import TYPE_CHECKING, Union | ||
|
||
import pytest | ||
|
||
from xarray import DataArray, Dataset, Variable | ||
|
||
if TYPE_CHECKING: | ||
from xarray.core.types import TypeAlias | ||
|
||
DimT: TypeAlias = Union[int, tuple, "DEnum", "CustomHashable"] | ||
|
||
|
||
class DEnum(Enum): | ||
dim = "dim" | ||
|
||
|
||
class CustomHashable: | ||
def __init__(self, a: int) -> None: | ||
self.a = a | ||
|
||
def __hash__(self) -> int: | ||
return self.a | ||
|
||
|
||
parametrize_dim = pytest.mark.parametrize( | ||
"dim", | ||
[ | ||
pytest.param(5, id="int"), | ||
pytest.param(("a", "b"), id="tuple"), | ||
pytest.param(DEnum.dim, id="enum"), | ||
pytest.param(CustomHashable(3), id="HashableObject"), | ||
], | ||
) | ||
|
||
|
||
@parametrize_dim | ||
def test_hashable_dims(dim: DimT) -> None: | ||
v = Variable([dim], [1, 2, 3]) | ||
da = DataArray([1, 2, 3], dims=[dim]) | ||
Dataset({"a": ([dim], [1, 2, 3])}) | ||
|
||
# alternative constructors | ||
DataArray(v) | ||
Dataset({"a": v}) | ||
Dataset({"a": da}) | ||
|
||
|
||
@parametrize_dim | ||
def test_dataset_variable_hashable_names(dim: DimT) -> None: | ||
Dataset({dim: ("x", [1, 2, 3])}) |