From 70155264df604f1a54602e52d338f51235e00b11 Mon Sep 17 00:00:00 2001 From: Patrick Kenny Date: Sat, 13 Aug 2022 21:44:43 -0400 Subject: [PATCH 1/4] Adds checks for nested data structures not of the same type as the outermost structure. --- src/_pytest/python_api.py | 4 +++- testing/python/approx.py | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 515d437f0..326283b4c 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -376,7 +376,9 @@ class ApproxSequenceLike(ApproxBase): def _check_type(self) -> None: __tracebackhide__ = True for index, x in enumerate(self.expected): - if isinstance(x, type(self.expected)): + if (isinstance(x, Collection) or isinstance(x, Mapping)) and not isinstance( + x, STRING_TYPES + ): msg = "pytest.approx() does not support nested data structures: {!r} at index {}\n full sequence: {}" raise TypeError(msg.format(x, index, pprint.pformat(self.expected))) diff --git a/testing/python/approx.py b/testing/python/approx.py index 6acb466ff..3bbccc862 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -781,6 +781,9 @@ class TestApprox: "x, name", [ pytest.param([[1]], "data structures", id="nested-list"), + pytest.param([(1,)], "data structures", id="nested-list-tuple"), + pytest.param([{1}], "data structures", id="nested-list-set"), + pytest.param([{"key": 1}], "data structures", id="nested-list-dict"), pytest.param({"key": {"key": 1}}, "dictionaries", id="nested-dict"), ], ) From 0d506dcc9c998d656d512c7269d8a92ed73a4d5a Mon Sep 17 00:00:00 2001 From: Patrick Kenny Date: Sat, 13 Aug 2022 21:55:20 -0400 Subject: [PATCH 2/4] Adds changelog, updates AUTHORS. --- AUTHORS | 1 + changelog/10210.bugfix.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/10210.bugfix.rst diff --git a/AUTHORS b/AUTHORS index e797f2146..9822b81cc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -265,6 +265,7 @@ Ondřej Súkup Oscar Benjamin Parth Patel Patrick Hayes +Patrick Kenny Paul Müller Paul Reece Pauli Virtanen diff --git a/changelog/10210.bugfix.rst b/changelog/10210.bugfix.rst new file mode 100644 index 000000000..7ef0fd341 --- /dev/null +++ b/changelog/10210.bugfix.rst @@ -0,0 +1 @@ +Fixes issue where nested data structures of different types did not raise an error with ``approx``. From f988bc41d8889e83cccab31ca445387b63793991 Mon Sep 17 00:00:00 2001 From: Oliver Bestwalter Date: Thu, 20 Jun 2024 15:23:50 +0200 Subject: [PATCH 3/4] STRING_TYPES is not necessary anymore --- src/_pytest/python_api.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 8f3747811..d6c66def2 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -376,9 +376,7 @@ class ApproxSequenceLike(ApproxBase): def _check_type(self) -> None: __tracebackhide__ = True for index, x in enumerate(self.expected): - if (isinstance(x, Collection) or isinstance(x, Mapping)) and not isinstance( - x, STRING_TYPES - ): + if (isinstance(x, Collection) or isinstance(x, Mapping)) and not isinstance(x, str): msg = "pytest.approx() does not support nested data structures: {!r} at index {}\n full sequence: {}" raise TypeError(msg.format(x, index, pprint.pformat(self.expected))) From 9d24ef71efe8607e93bbe2a028378571b316b90d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:24:25 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/python_api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index d6c66def2..b87518ae9 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -376,7 +376,9 @@ class ApproxSequenceLike(ApproxBase): def _check_type(self) -> None: __tracebackhide__ = True for index, x in enumerate(self.expected): - if (isinstance(x, Collection) or isinstance(x, Mapping)) and not isinstance(x, str): + if (isinstance(x, Collection) or isinstance(x, Mapping)) and not isinstance( + x, str + ): msg = "pytest.approx() does not support nested data structures: {!r} at index {}\n full sequence: {}" raise TypeError(msg.format(x, index, pprint.pformat(self.expected)))