Skip to content

Commit d7f445e

Browse files
authored
fix all-but-dask tests (#4511)
* fix all-but-dask tests * whats new * add dummy context
1 parent dfe5de7 commit d7f445e

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Internal Changes
6767
<https://github.com/mathause>`_.
6868
- Removed stray spaces that stem from black removing new lines (:pull:`4504`).
6969
By `Mathias Hauser <https://github.com/mathause>`_.
70+
- Ensure tests are not skipped in the `py38-all-but-dask` test environment
71+
(:issue:`4509`). By `Mathias Hauser <https://github.com/mathause>`_.
7072

7173
.. _whats-new.0.16.1:
7274

xarray/tests/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from xarray.core.indexing import ExplicitlyIndexed
1818
from xarray.core.options import set_options
1919
from xarray.testing import ( # noqa: F401
20+
assert_chunks_equal,
2021
assert_duckarray_allclose,
2122
assert_duckarray_equal,
2223
)
@@ -95,6 +96,39 @@ def LooseVersion(vstring):
9596

9697
dask.config.set(scheduler="single-threaded")
9798

99+
100+
class CountingScheduler:
101+
"""Simple dask scheduler counting the number of computes.
102+
103+
Reference: https://stackoverflow.com/questions/53289286/"""
104+
105+
def __init__(self, max_computes=0):
106+
self.total_computes = 0
107+
self.max_computes = max_computes
108+
109+
def __call__(self, dsk, keys, **kwargs):
110+
self.total_computes += 1
111+
if self.total_computes > self.max_computes:
112+
raise RuntimeError(
113+
"Too many computes. Total: %d > max: %d."
114+
% (self.total_computes, self.max_computes)
115+
)
116+
return dask.get(dsk, keys, **kwargs)
117+
118+
119+
@contextmanager
120+
def dummy_context():
121+
yield None
122+
123+
124+
def raise_if_dask_computes(max_computes=0):
125+
# return a dummy context manager so that this can be used for non-dask objects
126+
if not has_dask:
127+
return dummy_context()
128+
scheduler = CountingScheduler(max_computes)
129+
return dask.config.set(scheduler=scheduler)
130+
131+
98132
flaky = pytest.mark.flaky
99133
network = pytest.mark.network
100134

xarray/tests/test_accessor_dt.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
from . import (
88
assert_array_equal,
9+
assert_chunks_equal,
910
assert_equal,
1011
assert_identical,
12+
raise_if_dask_computes,
1113
raises_regex,
1214
requires_cftime,
1315
requires_dask,
1416
)
15-
from .test_dask import assert_chunks_equal, raise_if_dask_computes
1617

1718

1819
class TestDatetimeAccessor:

xarray/tests/test_dask.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
assert_equal,
2424
assert_frame_equal,
2525
assert_identical,
26+
raise_if_dask_computes,
2627
raises_regex,
2728
requires_pint_0_15,
2829
requires_scipy_or_netCDF4,
@@ -36,30 +37,6 @@
3637
ON_WINDOWS = sys.platform == "win32"
3738

3839

39-
class CountingScheduler:
40-
"""Simple dask scheduler counting the number of computes.
41-
42-
Reference: https://stackoverflow.com/questions/53289286/"""
43-
44-
def __init__(self, max_computes=0):
45-
self.total_computes = 0
46-
self.max_computes = max_computes
47-
48-
def __call__(self, dsk, keys, **kwargs):
49-
self.total_computes += 1
50-
if self.total_computes > self.max_computes:
51-
raise RuntimeError(
52-
"Too many computes. Total: %d > max: %d."
53-
% (self.total_computes, self.max_computes)
54-
)
55-
return dask.get(dsk, keys, **kwargs)
56-
57-
58-
def raise_if_dask_computes(max_computes=0):
59-
scheduler = CountingScheduler(max_computes)
60-
return dask.config.set(scheduler=scheduler)
61-
62-
6340
def test_raise_if_dask_computes():
6441
data = da.from_array(np.random.RandomState(0).randn(4, 6), chunks=(2, 2))
6542
with raises_regex(RuntimeError, "Too many computes"):

xarray/tests/test_dataarray.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
assert_equal,
3333
assert_identical,
3434
has_dask,
35+
raise_if_dask_computes,
3536
raises_regex,
3637
requires_bottleneck,
3738
requires_dask,
@@ -42,8 +43,6 @@
4243
source_ndarray,
4344
)
4445

45-
from .test_dask import raise_if_dask_computes
46-
4746

4847
class TestDataArray:
4948
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)