Skip to content

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

@dpanici

Description

@dpanici

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions