Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #1298 on branch 0.10.x (Fix test compatibility with Pytest 8) #1326

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions anndata/tests/test_io_warnings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import warnings
from importlib.util import find_spec
from pathlib import Path
Expand All @@ -14,10 +15,26 @@
def test_old_format_warning_thrown():
import scanpy as sc

with pytest.warns(ad._warnings.OldFormatWarning):
pth = Path(sc.datasets.__file__).parent / "10x_pbmc68k_reduced.h5ad"
pth = Path(sc.datasets.__file__).parent / "10x_pbmc68k_reduced.h5ad"
# TODO: with Pytest 8, all this can be a
# `with pytest.warns(...), pytest.warns(...):`
with warnings.catch_warnings(record=True) as record:
warnings.simplefilter("always", ad.OldFormatWarning)
warnings.simplefilter("always", FutureWarning)
ad.read_h5ad(pth)

assert any(issubclass(w.category, ad.OldFormatWarning) for w in record), [
w.message for w in record if not issubclass(w.category, FutureWarning)
]
assert any(
issubclass(w.category, FutureWarning)
and re.match(
r"Moving element from \.uns\['neighbors']\['distances'] to \.obsp\['distances']\.",
str(w.message),
)
for w in record
), [w.message for w in record if not issubclass(w.category, ad.OldFormatWarning)]


def test_old_format_warning_not_thrown(tmp_path):
pth = tmp_path / "current.h5ad"
Expand Down
14 changes: 7 additions & 7 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
from pathlib import Path


doctest_marker = pytest.mark.usefixtures("doctest_env")
@pytest.fixture(autouse=True)
def _suppress_env_for_doctests(request: pytest.FixtureRequest) -> None:
if isinstance(request.node, pytest.DoctestItem):
request.getfixturevalue("_doctest_env")


@pytest.fixture
def doctest_env(
@pytest.fixture()
def _doctest_env(
request: pytest.FixtureRequest, cache: pytest.Cache, tmp_path: Path
) -> Generator[None, None, None]:
from scanpy import settings
Expand All @@ -44,7 +47,7 @@ def doctest_env(


def pytest_itemcollected(item: pytest.Item) -> None:
"""Define behavior of pytest.mark.gpu and doctests."""
"""Define behavior of pytest.mark.gpu."""
from importlib.util import find_spec

is_gpu = len([mark for mark in item.iter_markers(name="gpu")]) > 0
Expand All @@ -53,9 +56,6 @@ def pytest_itemcollected(item: pytest.Item) -> None:
pytest.mark.skipif(not find_spec("cupy"), reason="Cupy not installed.")
)

if isinstance(item, pytest.DoctestItem):
item.add_marker(doctest_marker)


def pytest_addoption(parser: pytest.Parser) -> None:
"""Hook to register custom CLI options and config values"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ doc = [
]
test = [
"loompy>=3.0.5",
"pytest >=6.0, !=8.0.0rc1", # https://github.com/pytest-dev/pytest/issues/11759
"pytest >=6.0",
"pytest-cov>=2.10",
"zarr",
"matplotlib",
Expand Down