Skip to content

Commit

Permalink
Centralize warning config
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Oct 30, 2023
1 parent 12d0f41 commit 8b64def
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
16 changes: 2 additions & 14 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,8 @@ jobs:
displayName: "PyTest (coverage)"
condition: and(eq(variables['RUN_COVERAGE'], 'yes'), eq(variables['PRERELEASE_DEPENDENCIES'], 'no'))
# TODO: fix all the exceptions here
# TODO: Centralize, see https://github.com/scverse/anndata/issues/1204
- script: >
pytest
-W error
-W 'ignore:Support for Awkward Arrays is currently experimental'
-W 'ignore:Outer joins on awkward.Arrays'
-W 'default:Setting element:UserWarning'
-W 'default:Trying to modify attribute:UserWarning'
-W 'default:Transforming to str index:UserWarning'
-W 'default:Observation names are not unique. To make them unique:UserWarning'
-W 'default:Variable names are not unique. To make them unique:UserWarning'
-W 'default::scipy.sparse._base.SparseEfficiencyWarning'
-W 'default::dask.array.core.PerformanceWarning'
- script: |
pytest --strict-warnings
displayName: "PyTest (treat warnings as errors)"
condition: and(eq(variables['RUN_COVERAGE'], 'no'), eq(variables['PRERELEASE_DEPENDENCIES'], 'yes'))
Expand Down
48 changes: 45 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@
from anndata.utils import import_name

if TYPE_CHECKING:
from collections.abc import Generator, Iterable
from pathlib import Path


doctest_marker = pytest.mark.usefixtures("doctest_env")


@pytest.fixture
def doctest_env(
request: pytest.FixtureRequest, cache: pytest.Cache, tmp_path: Path
) -> None:
) -> Generator[None, None, None]:
from scanpy import settings

assert isinstance(request.node.parent, pytest.Module)
# request.node.parent is either a DoctestModule or a DoctestTextFile.
# Only DoctestModule has a .obj attribute (the imported module).
if request.node.parent.obj:
func = import_name(request.node.name)
warning_detail: tuple[type[Warning], str, bool] | None
if warning_detail := getattr(func, "__deprecated", None):
cat, msg, _ = warning_detail # type: tuple[type[Warning], str, bool]
cat, msg, _ = warning_detail
warnings.filterwarnings("ignore", category=cat, message=re.escape(msg))

old_dd, settings.datasetdir = settings.datasetdir, cache.mkdir("scanpy-data")
Expand All @@ -39,7 +43,7 @@ def doctest_env(
settings.datasetdir = old_dd


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

Expand All @@ -51,3 +55,41 @@ def pytest_itemcollected(item):

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


def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption(
"--strict-warnings",
action="store_true",
default=False,
help="Turn most warnings into errors",
)


def pytest_collection_modifyitems(
session: pytest.Session, config: pytest.Config, items: Iterable[pytest.Item]
):
if not config.getoption("--strict-warnings"):
return

filters_from_config: list[str] = []
if fs := config.getini("filterwarnings"):
assert isinstance(fs, list)
filters_from_config = [str(f) for f in fs]

warning_filters = [
r"error",
*filters_from_config,
r"default::anndata._warnings.ImplicitModificationWarning",
r"default:Transforming to str index:UserWarning",
r"default:(Observation|Variable) names are not unique. To make them unique:UserWarning",
r"default::scipy.sparse.SparseEfficiencyWarning",
r"default::dask.array.core.PerformanceWarning",
]
warning_marks = [pytest.mark.filterwarnings(f) for f in warning_filters]

for item in items:
# reverse and then prepend means essentially `marks[0:0] = warning_marks`
# this ensures that markers that are applied later override these
for mark in reversed(warning_marks):
item.add_marker(mark, append=False)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ addopts = [
"--ignore=anndata/core.py", # deprecated
"--ignore=anndata/readwrite.py", # deprecated
]
# See conftest.py for the warning filters that get applied when --strict-warnings in passed
filterwarnings = [
'ignore:Support for Awkward Arrays is currently experimental',
'ignore:Outer joins on awkward\.Arrays',
'ignore::anndata._warnings.ExperimentalFeatureWarning',
]
python_files = "test_*.py"
testpaths = ["anndata", "docs/concatenation.rst"]
Expand Down

0 comments on commit 8b64def

Please sign in to comment.