From bffead8c59484fcac4b469d8069fe0d84c59738b Mon Sep 17 00:00:00 2001 From: Arya Rizky Date: Wed, 13 May 2026 07:56:01 +0700 Subject: [PATCH 1/2] Fix _as_numpy_array: use variable obj instead of string literal "obj" 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. Closes #14456 --- 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..b72d0136484 100644 --- a/AUTHORS +++ b/AUTHORS @@ -21,6 +21,7 @@ Alex Lambson Alexander Johnson Alexander King Alexei Kozlenok +algojogacor Alice Purcell Allan Feldman Aly Sivji diff --git a/changelog/14456.bugfix.rst b/changelog/14456.bugfix.rst new file mode 100644 index 00000000000..c99e82a5a4a --- /dev/null +++ b/changelog/14456.bugfix.rst @@ -0,0 +1 @@ +Fixed a bug in :func:`pytest.approx` where ``_as_numpy_array`` incorrectly used a string literal ``"obj"`` instead of the variable ``obj`` when checking for ``__array_interface__``, causing objects implementing only ``__array_interface__`` (without ``__array__``) to not be recognized as numpy-like arrays. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index cc2a2b7126a..f22a0f5016d 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -925,6 +925,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 From 8bae589cfba6aa7f17e621e5d89b05004303b0b8 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 14 May 2026 12:23:39 +0300 Subject: [PATCH 2/2] Update changelog/14456.bugfix.rst --- changelog/14456.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/14456.bugfix.rst b/changelog/14456.bugfix.rst index c99e82a5a4a..ccd9ca20a8f 100644 --- a/changelog/14456.bugfix.rst +++ b/changelog/14456.bugfix.rst @@ -1 +1 @@ -Fixed a bug in :func:`pytest.approx` where ``_as_numpy_array`` incorrectly used a string literal ``"obj"`` instead of the variable ``obj`` when checking for ``__array_interface__``, causing objects implementing only ``__array_interface__`` (without ``__array__``) to not be recognized as numpy-like arrays. +Fixed :func:`pytest.approx` not recognizing types with ``__array_interface__`` as numpy-like arrays.