Skip to content

Commit 7b76f16

Browse files
crusaderkymax-sixty
authored andcommitted
Fix distributed.Client.compute applied to DataArray (#3173)
* Fix distributed.Client.compute applied to DataArray * type annotations
1 parent 1757dff commit 7b76f16

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

doc/whats-new.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ Bug fixes
5555
due to a ``datetime`` issue in NumPy (:issue:`2334`).
5656
By `Graham Inggs <https://github.com/ginggs>`_.
5757
- Fixed bug in ``combine_by_coords()`` causing a `ValueError` if the input had
58-
an unused dimension with coordinates which were not monotonic (:issue`3150`).
58+
an unused dimension with coordinates which were not monotonic (:issue:`3150`).
5959
By `Tom Nicholas <http://github.com/TomNicholas>`_.
60+
- Fixed crash when applying ``distributed.Client.compute()`` to a DataArray
61+
(:issue:`3171`). By `Guido Imperiale <https://github.com/crusaderky>`_.
62+
6063

6164
.. _whats-new.0.12.3:
6265

xarray/core/dataarray.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def _replace(
300300
self,
301301
variable: Variable = None,
302302
coords=None,
303-
name: Union[Hashable, None, ReprObject] = __default,
303+
name: Optional[Hashable] = __default,
304304
) -> 'DataArray':
305305
if variable is None:
306306
variable = self.variable
@@ -313,7 +313,7 @@ def _replace(
313313
def _replace_maybe_drop_dims(
314314
self,
315315
variable: Variable,
316-
name: Union[str, None, utils.ReprObject] = __default
316+
name: Optional[Hashable] = __default
317317
) -> 'DataArray':
318318
if variable.dims == self.dims and variable.shape == self.shape:
319319
coords = self._coords.copy()
@@ -356,7 +356,7 @@ def _to_temp_dataset(self) -> Dataset:
356356
def _from_temp_dataset(
357357
self,
358358
dataset: Dataset,
359-
name: Union[Hashable, ReprObject] = __default
359+
name: Hashable = __default
360360
) -> 'DataArray':
361361
variable = dataset._variables.pop(_THIS_ARRAY)
362362
coords = dataset._variables

xarray/core/utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,22 @@ def __repr__(self: Any) -> str:
462462
class ReprObject:
463463
"""Object that prints as the given value, for use with sentinel values.
464464
"""
465+
__slots__ = ('_value', )
466+
465467
def __init__(self, value: str):
466468
self._value = value
467469

468470
def __repr__(self) -> str:
469471
return self._value
470472

473+
def __eq__(self, other) -> bool:
474+
if isinstance(other, ReprObject):
475+
return self._value == other._value
476+
return False
477+
478+
def __hash__(self) -> int:
479+
return hash((ReprObject, self._value))
480+
471481

472482
@contextlib.contextmanager
473483
def close_on_error(f):

xarray/tests/test_utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22
from datetime import datetime
3+
from typing import Hashable
34

45
import numpy as np
56
import pandas as pd
@@ -179,6 +180,21 @@ def test_sorted_keys_dict(self):
179180
def test_repr_object():
180181
obj = utils.ReprObject('foo')
181182
assert repr(obj) == 'foo'
183+
assert isinstance(obj, Hashable)
184+
assert not isinstance(obj, str)
185+
186+
187+
def test_repr_object_magic_methods():
188+
o1 = utils.ReprObject('foo')
189+
o2 = utils.ReprObject('foo')
190+
o3 = utils.ReprObject('bar')
191+
o4 = 'foo'
192+
assert o1 == o2
193+
assert o1 != o3
194+
assert o1 != o4
195+
assert hash(o1) == hash(o2)
196+
assert hash(o1) != hash(o3)
197+
assert hash(o1) != hash(o4)
182198

183199

184200
def test_is_remote_uri():

0 commit comments

Comments
 (0)