From 50fcba5cea8c72af0c652a9fe375533ebc8efeaf Mon Sep 17 00:00:00 2001 From: Harsh Kumar Date: Sun, 10 May 2026 12:25:51 +0530 Subject: [PATCH] Fix hasattr typo in _as_numpy_array: use variable obj instead of string "obj" Closes #14456 The second hasattr call in _as_numpy_array incorrectly passed the string literal "obj" instead of the variable obj when checking for __array_interface__. This caused objects implementing only __array_interface__ (without __array__) to not be recognized as numpy-like arrays by pytest.approx. Co-authored-by: Kiro (AI) --- AUTHORS | 1 + changelog/14456.bugfix.rst | 1 + src/_pytest/python_api.py | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelog/14456.bugfix.rst diff --git a/AUTHORS b/AUTHORS index f3cf1b0facb..4186bd5abf8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -197,6 +197,7 @@ Guido Wesdorp Guoqiang Zhang Hamza Mobeen Harald Armin Massa +Harsh Kumar Harshna Henk-Jaap Wagenaar Holger Kohr diff --git a/changelog/14456.bugfix.rst b/changelog/14456.bugfix.rst new file mode 100644 index 00000000000..9f259c686da --- /dev/null +++ b/changelog/14456.bugfix.rst @@ -0,0 +1 @@ +Fixed ``hasattr`` call in ``_as_numpy_array`` that incorrectly used the string literal ``"obj"`` instead of the variable ``obj`` when checking for ``__array_interface__``, causing objects implementing only ``__array_interface__`` to not be recognized as numpy-like arrays by ``pytest.approx``. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index f6d5e31a588..b94752d2eab 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -906,6 +906,6 @@ def _as_numpy_array(obj: object) -> ndarray | None: return None elif isinstance(obj, np.ndarray): return obj - elif hasattr(obj, "__array__") or hasattr("obj", "__array_interface__"): + elif hasattr(obj, "__array__") or hasattr(obj, "__array_interface__"): return np.asarray(obj) return None