Skip to content

Commit 082d9f6

Browse files
committed
Add hypothesis workflow
1 parent 15e00ff commit 082d9f6

File tree

4 files changed

+106
-8
lines changed

4 files changed

+106
-8
lines changed

.github/workflows/ci-additional.yaml

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ jobs:
3535
runs-on: "ubuntu-latest"
3636
needs: detect-ci-trigger
3737
if: needs.detect-ci-trigger.outputs.triggered == 'false'
38+
3839
defaults:
3940
run:
4041
shell: bash -l {0}
41-
4242
env:
4343
CONDA_ENV_FILE: ci/requirements/environment.yml
4444
PYTHON_VERSION: "3.11"
45-
4645
steps:
4746
- uses: actions/checkout@v4
4847
with:
@@ -82,6 +81,78 @@ jobs:
8281
# [MHS, 01/25/2024] Skip datatree_ documentation remove after #8572
8382
python -m pytest --doctest-modules xarray --ignore xarray/tests --ignore xarray/datatree_ -Werror
8483
84+
hypothesis:
85+
name: Slow Hypothesis Tests
86+
runs-on: "ubuntu-latest"
87+
needs: detect-ci-trigger
88+
if: |
89+
always()
90+
&& (
91+
(github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
92+
|| needs.detect-ci-trigger.outputs.triggered == 'true'
93+
|| contains( github.event.pull_request.labels.*.name, 'run-slow-hypothesis')
94+
)
95+
defaults:
96+
run:
97+
shell: bash -l {0}
98+
99+
env:
100+
CONDA_ENV_FILE: ci/requirements/environment.yml
101+
PYTHON_VERSION: "3.12"
102+
103+
steps:
104+
- uses: actions/checkout@v4
105+
with:
106+
fetch-depth: 0 # Fetch all history for all branches and tags.
107+
108+
- name: set environment variables
109+
run: |
110+
echo "TODAY=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
111+
112+
- name: Setup micromamba
113+
uses: mamba-org/setup-micromamba@v1
114+
with:
115+
environment-file: ${{env.CONDA_ENV_FILE}}
116+
environment-name: xarray-tests
117+
create-args: >-
118+
python=${{env.PYTHON_VERSION}}
119+
conda
120+
cache-environment: true
121+
cache-environment-key: "${{runner.os}}-${{runner.arch}}-py${{env.PYTHON_VERSION}}-${{env.TODAY}}-${{hashFiles(env.CONDA_ENV_FILE)}}"
122+
123+
- name: Install xarray
124+
run: |
125+
python -m pip install --no-deps -e .
126+
- name: Version info
127+
run: |
128+
conda info -a
129+
conda list
130+
python xarray/util/print_versions.py
131+
- name: Restore cached hypothesis directory
132+
uses: actions/cache@v4
133+
with:
134+
path: .hypothesis/
135+
key: cache-hypothesis
136+
enableCrossOsArchive: true
137+
save-always: true
138+
- name: Run slow Hypothesis tests
139+
if: success()
140+
id: status
141+
run: |
142+
python -m pytest --hypothesis-show-statistics --run-slow-hypothesis properties/*.py \
143+
--report-log output-${{ matrix.python-version }}-log.jsonl
144+
- name: Generate and publish the report
145+
if: |
146+
failure()
147+
&& steps.status.outcome == 'failure'
148+
&& github.event_name == 'schedule'
149+
&& github.repository_owner == 'pydata'
150+
uses: xarray-contrib/issue-from-pytest-log@v1
151+
with:
152+
log-path: output-${{ matrix.python-version }}-log.jsonl
153+
issue-title: "Nightly Hypothesis tests failed"
154+
issue-label: "topic-hypothesis"
155+
85156
mypy:
86157
name: Mypy
87158
runs-on: "ubuntu-latest"

properties/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
import pytest
2+
3+
4+
def pytest_addoption(parser):
5+
parser.addoption(
6+
"--run-slow-hypothesis",
7+
action="store_true",
8+
default=False,
9+
help="run slow hypothesis tests",
10+
)
11+
12+
13+
def pytest_collection_modifyitems(config, items):
14+
if config.getoption("--run-slow-hypothesis"):
15+
return
16+
skip_slow_hyp = pytest.mark.skip(reason="need --run-slow-hypothesis option to run")
17+
for item in items:
18+
if "slow_hypothesis" in item.keywords:
19+
item.add_marker(skip_slow_hyp)
20+
21+
122
try:
223
from hypothesis import settings
324
except ImportError:

properties/test_index_manipulation.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from xarray.testing import _assert_internal_invariants
88

99
pytest.importorskip("hypothesis")
10+
pytestmark = pytest.mark.slow_hypothesis
1011

1112
import hypothesis.strategies as st
1213
from hypothesis import note, settings
@@ -52,12 +53,7 @@ def __init__(self):
5253
self.multi_indexed_dims = []
5354

5455
# TODO: stacking with a timedelta64 index and unstacking converts it to object
55-
@rule(
56-
var=xrst.index_variables(
57-
dims=DIM_NAME,
58-
dtype=xrst.pandas_index_dtypes().filter(lambda x: x.kind != "m"),
59-
)
60-
)
56+
@rule(var=xrst.index_variables(dims=DIM_NAME, dtype=xrst.pandas_index_dtypes()))
6157
def add_dim_coord(self, var):
6258
(name,) = var.dims
6359
note(f"adding dimension coordinate {name}")
@@ -253,3 +249,12 @@ def test_unstack_object():
253249
ds = xr.Dataset()
254250
ds["0"] = np.array(["", "\x000"], dtype=object)
255251
ds.stack({"1": ["0"]}).unstack()
252+
253+
254+
@pytest.mark.skip(reason="failure detected by hypothesis")
255+
def test_unstack_timedelta_index():
256+
import xarray as xr
257+
258+
ds = xr.Dataset()
259+
ds["0"] = np.array([0, 1, 2, 3], dtype="timedelta64[ns]")
260+
ds.stack({"1": ["0"]}).unstack()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ markers = [
295295
"flaky: flaky tests",
296296
"network: tests requiring a network connection",
297297
"slow: slow tests",
298+
"slow_hypothesis: slow hypothesis tests",
298299
]
299300
minversion = "7"
300301
python_files = "test_*.py"

0 commit comments

Comments
 (0)