Skip to content

Commit

Permalink
Backport PR #1298 on branch 0.10.x (Fix test compatibility with Pytes…
Browse files Browse the repository at this point in the history
…t 8) (#1326)

Co-authored-by: Philipp A <[email protected]>
  • Loading branch information
meeseeksmachine and flying-sheep authored Jan 23, 2024
1 parent fe5b12a commit 560076d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
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

0 comments on commit 560076d

Please sign in to comment.