pytest-dev / pytest-dev/pytest

Trying to filter and ignore a caught `ImportError` inside source project which we then emit a `UserWarning` for

Open
#13,838 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: warnings
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Maybe related to #11483, in my code (like my source code, not inside of any testing files for pytest) I have a section that checks for an optional dependency and throws a warning if it is not installed, something like

import warnings
try:
    import optional
except ImportError:
    warnings.warn("optional dependency is not installed")

this emits a UserWarning, however in my setup.cfg I try to ignore this benign warning but still raise errors on other warnings:

filterwarnings=
    error
    # dont have pytest fail on optional dependency warning
    ignore:optional:UserWarning

but this does not seem to work, and throws an error still like this (this is from my real production code, so does not line up with the MWE above, but a situation exactly like the above is I think what is causing this issue):

ImportError while loading conftest '/home/dpanici/DESC/tests/conftest.py'.
tests/conftest.py:13: in <module>
    from desc.coils import (
desc/coils.py:24: in <module>
    from desc.compute import get_params
desc/compute/__init__.py:30: in <module>
    from . import (
desc/compute/_bootstrap.py:16: in <module>
    from ..integrals.surface_integral import surface_averages_map
desc/integrals/__init__.py:3: in <module>
    from ._bounce_utils import fast_chebyshev, fast_cubic_spline, fourier_chebyshev
desc/integrals/_bounce_utils.py:8: in <module>
    from desc.integrals._interp_utils import (
desc/integrals/_interp_utils.py:19: in <module>
    warnings.warn(
E   UserWarning:
E   jax-finufft is not installed.
E   If you want to use NUFFTs, follow the DESC installation instructions.
E   Otherwise you must set the parameter nufft_eps to zero
E   when computing effective ripple, Gamma_c, and any other
E   computations that involve bounce integrals.

Is there a way for me to have pytest not fail at catching this caught importerror-leading-to-warning? It seems like it is finding the raised ImportError despite it being caught and handled with the try/except block.

Thanks! I am using pytest versions:

pytest                        8.4.1
pytest-benchmark              5.1.0
pytest-cov                    6.2.1
pytest-monitor                1.6.6
pytest-mpl                    0.16.1
pytest-split                  0.10.0
pytest-timeout                2.4.0

on a Linux OS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning-filtering setup from setup.cfg with pytest 8.4.1, including the optional import and the warning emitted while loading conftest.py. Start by checking how pytest handles warnings during conftest imports and whether the shown filter pattern is applied. Done means identifying whether this is incorrect behavior or configuration, with a focused regression test or documented explanation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.