Skip to content

Commit ecc55b4

Browse files
authored
pytest.approx: import numpy conditionally (#13563)
This matches existing conditional-import behavior in `pytest.approx`, and improves runtime for users of `pytest.approx` and not `numpy`.
1 parent ae73461 commit ecc55b4

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Leonardus Chen
261261
Lev Maximov
262262
Levon Saldamli
263263
Lewis Cowles
264+
Liam DeVoe
264265
Llandy Riveron Del Risco
265266
Loic Esteve
266267
lovetheguitar

changelog/13563.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`.

src/_pytest/python_api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,9 @@ def is_bool(val: Any) -> bool:
426426
# Check if `val` is a native bool or numpy bool.
427427
if isinstance(val, bool):
428428
return True
429-
try:
430-
import numpy as np
431-
429+
if np := sys.modules.get("numpy"):
432430
return isinstance(val, np.bool_)
433-
except ImportError:
434-
return False
431+
return False
435432

436433
asarray = _as_numpy_array(actual)
437434
if asarray is not None:

0 commit comments

Comments
 (0)