From a17320c129a46a1e6738aab95a5652443269c798 Mon Sep 17 00:00:00 2001 From: arya rizky Date: Thu, 14 May 2026 16:40:33 +0700 Subject: [PATCH] approx: fix buggy `__array_interface__` check Fix #14456 (cherry picked from commit 21be7a259424e509e3d667082a138bcc8fc45c00) --- 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 620ed218886..af895ed1855 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..ccd9ca20a8f --- /dev/null +++ b/changelog/14456.bugfix.rst @@ -0,0 +1 @@ +Fixed :func:`pytest.approx` not recognizing types with ``__array_interface__`` as numpy-like arrays. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index bab70aa4a8c..1ff35e94fcf 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -814,6 +814,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