diff --git a/AUTHORS b/AUTHORS index e5b863e71f1..df338aa8c6e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -261,6 +261,7 @@ Leonardus Chen Lev Maximov Levon Saldamli Lewis Cowles +Liam DeVoe Llandy Riveron Del Risco Loic Esteve lovetheguitar diff --git a/changelog/13563.bugfix.rst b/changelog/13563.bugfix.rst new file mode 100644 index 00000000000..543552e20cf --- /dev/null +++ b/changelog/13563.bugfix.rst @@ -0,0 +1 @@ +:func:`pytest.approx` now only imports ``numpy`` if NumPy is already in ``sys.modules``. This fixes unconditional import behavior introduced in `8.4.0`. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index b732b452683..52e564bd809 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -426,12 +426,9 @@ def is_bool(val: Any) -> bool: # Check if `val` is a native bool or numpy bool. if isinstance(val, bool): return True - try: - import numpy as np - + if np := sys.modules.get("numpy"): return isinstance(val, np.bool_) - except ImportError: - return False + return False asarray = _as_numpy_array(actual) if asarray is not None: