Skip to content

Commit a029026

Browse files
author
Guido Imperiale
committed
Replace utils.hashable(x) with isinstance(x, Hashable)
1 parent 5299aa7 commit a029026

File tree

4 files changed

+5
-23
lines changed

4 files changed

+5
-23
lines changed

xarray/core/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .options import OPTIONS, _get_keep_attrs
3434
from .pycompat import dask_array_type
3535
from .utils import (Frozen, SortedKeysDict, _check_inplace,
36-
decode_numpy_dict_values, either_dict_or_kwargs, hashable,
36+
decode_numpy_dict_values, either_dict_or_kwargs,
3737
maybe_wrap_array)
3838
from .variable import IndexVariable, Variable, as_variable, broadcast_variables
3939

@@ -1096,7 +1096,7 @@ def __getitem__(self, key: object) -> 'Union[DataArray, Dataset]':
10961096
if utils.is_dict_like(key):
10971097
return self.isel(**cast(Mapping, key))
10981098

1099-
if hashable(key):
1099+
if isinstance(key, Hashable):
11001100
return self._construct_dataarray(key)
11011101
else:
11021102
return self._copy_listed(np.asarray(key))

xarray/core/groupby.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import functools
33
import warnings
4+
from typing import Hashable
45

56
import numpy as np
67
import pandas as pd
@@ -11,7 +12,7 @@
1112
from .common import ALL_DIMS, ImplementsArrayReduce, ImplementsDatasetReduce
1213
from .options import _get_keep_attrs
1314
from .pycompat import integer_types
14-
from .utils import hashable, maybe_wrap_array, peek_at, safe_cast_to_index
15+
from .utils import maybe_wrap_array, peek_at, safe_cast_to_index
1516
from .variable import IndexVariable, Variable, as_variable
1617

1718

@@ -228,7 +229,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
228229
raise TypeError("can't specify both `grouper` and `bins`")
229230

230231
if not isinstance(group, (DataArray, IndexVariable)):
231-
if not hashable(group):
232+
if not isinstance(group, Hashable):
232233
raise TypeError('`group` must be an xarray.DataArray or the '
233234
'name of an xarray variable or dimension')
234235
group = obj[group]

xarray/core/utils.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,6 @@ def is_uniform_spaced(arr, **kwargs) -> bool:
515515
return bool(np.isclose(diffs.min(), diffs.max(), **kwargs))
516516

517517

518-
def hashable(v: Any) -> bool:
519-
"""Determine whether `v` can be hashed.
520-
"""
521-
try:
522-
hash(v)
523-
except TypeError:
524-
return False
525-
return True
526-
527-
528518
def not_implemented(*args, **kwargs):
529519
return NotImplemented
530520

xarray/tests/test_utils.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,6 @@ def test_relative_tolerance(self):
234234
assert utils.is_uniform_spaced([0, 0.97, 2], rtol=0.1)
235235

236236

237-
class Test_hashable:
238-
239-
def test_hashable(self):
240-
for v in [False, 1, (2, ), (3, 4), 'four']:
241-
assert utils.hashable(v)
242-
for v in [[5, 6], ['seven', '8'], {9: 'ten'}]:
243-
assert not utils.hashable(v)
244-
245-
246237
@requires_dask
247238
def test_dask_array_is_scalar():
248239
# regression test for GH1684

0 commit comments

Comments
 (0)