diff --git a/AUTHORS b/AUTHORS index 7fd388505..316d5fcc5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -318,6 +318,7 @@ Ondřej Súkup Oscar Benjamin Parth Patel Patrick Hayes +Patrick Kenny Patrick Lannigan Paul Müller Paul Reece 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``. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 4174a55b5..df201ff5f 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, 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))) diff --git a/testing/python/approx.py b/testing/python/approx.py index 69743cdbe..6bf1794d9 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -841,6 +841,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"), ], )