Skip to content

Commit

Permalink
Fixes for pytest-8.0.0 compatibility (#8686)
Browse files Browse the repository at this point in the history
* test_dataset: remove incorrect pytest.warns() to fix pytest-8

Remove two incorrect `pytest.warns()` assertions to fix test failures
with pytest-8.0.0.  Prior to this version, an exception raised would
cause `pytest.warns()` to be ignored.  This way fixed in 8.0.0, and now
warnings must actually be emitted prior to the exception.

In `test_drop_index_labels()`, the exception is raised at the very
beginning of the function, prior to the deprecation warning.

In `test_rename_multiindex()`, the warning is not emitted at all (it is
not applicable to the call in question).

* test_groupby: Clear recorded warnings for pytest-8 compatibility

Clear the warnings recorded during the `pytest.warns()` use
in `test_groupby_dims_property`, to fix test failures with pytest-8.0.0.
Prior to this version, `pytest.warns()` invocation used to capture all
warnings.  Now it only captures the warnings that match the arguments,
and the remaining warnings are re-emitted and therefore caught by
`recwarn` fixture.  To provide compatibility with both versions of
pytest, clear the recorded warnings immediately after `pytest.warns()`.

Fixes #8681

* Revert "Fix CI: temporary pin pytest version to 7.4.* (#8682)"

This reverts commit b0b5b2f.
The tests should be fixed now.
  • Loading branch information
mgorny authored Jan 31, 2024
1 parent 614c25b commit ca10531
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ci/requirements/all-but-dask.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
- pint>=0.22
- pip
- pydap
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/bare-minimum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- python=3.9
- coveralls
- pip
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/environment-3.12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies:
- pooch
- pre-commit
- pydap
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/environment-windows-3.12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies:
- pip
- pre-commit
- pydap
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/environment-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies:
- pip
- pre-commit
- pydap
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:
- pre-commit
- pyarrow # pandas makes a deprecation warning without this, breaking doctests
- pydap
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/min-all-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies:
- pint=0.22
- pip
- pydap=3.3
- pytest==7.4.*
- pytest
- pytest-cov
- pytest-env
- pytest-xdist
Expand Down
6 changes: 2 additions & 4 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2724,8 +2724,7 @@ def test_drop_index_labels(self) -> None:
assert_identical(data, actual)

with pytest.raises(ValueError):
with pytest.warns(DeprecationWarning):
data.drop(["c"], dim="x", errors="wrong_value") # type: ignore[arg-type]
data.drop(["c"], dim="x", errors="wrong_value") # type: ignore[arg-type]

with pytest.warns(DeprecationWarning):
actual = data.drop(["a", "b", "c"], "x", errors="ignore")
Expand Down Expand Up @@ -3159,8 +3158,7 @@ def test_rename_multiindex(self) -> None:
original.rename({"a": "x"})

with pytest.raises(ValueError, match=r"'b' conflicts"):
with pytest.warns(UserWarning, match="does not create an index anymore"):
original.rename({"a": "b"})
original.rename({"a": "b"})

def test_rename_perserve_attrs_encoding(self) -> None:
# test propagate attrs/encoding to new variable(s) created from Index object
Expand Down
2 changes: 2 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def test_groupby_dims_property(dataset, recwarn) -> None:
with pytest.warns(UserWarning, match="The `squeeze` kwarg"):
assert dataset.groupby("x").dims == dataset.isel(x=1).dims
assert dataset.groupby("y").dims == dataset.isel(y=1).dims
# in pytest-8, pytest.warns() no longer clears all warnings
recwarn.clear()

# when squeeze=False, no warning should be raised
assert tuple(dataset.groupby("x", squeeze=False).dims) == tuple(
Expand Down

0 comments on commit ca10531

Please sign in to comment.