pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.89k stars 2.65k forks source link

`pytest.approx` does not raise in mixed data type nested structures #10210

Open jkittner opened 2 years ago

jkittner commented 2 years ago

description

when using pytest.approx on nested data structures I'd expect it to raise an exception also for mixed data type containers like a tuple in a list. So far it only works for the same data types e.g. lists in lists, tuples in tuples. For mixed data structures it incorrectly returns False, even though they should match which is a little misleading (also see below).

output of pip list

(venv) kittnjdr@ububox:~/workspace/pytest$ pip list
Package    Version                 Editable project location
---------- ----------------------- -------------------------------
attrs      22.1.0
iniconfig  1.1.1
packaging  21.3
pip        22.1.2
pluggy     1.0.0
py         1.11.0
pyparsing  3.0.9
pytest     7.2.0.dev232+gcc0092b9d /home/kittnjdr/workspace/pytest
setuptools 63.2.0
tomli      2.0.1
wheel      0.37.1

versions

These test cases fail, but should pass with the potential bug fixed.

diff --git a/testing/python/approx.py b/testing/python/approx.py
index 6acb466ff..31dd4638b 100644
--- a/testing/python/approx.py
+++ b/testing/python/approx.py
@@ -781,6 +781,10 @@ 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"),
         ],
     )
ptkenny commented 2 years ago

I'm gonna try taking a look at this one, will update if I get stuck(this is my first issue in this project).

jkittner commented 2 years ago

This might be a tricky one, since some fundamental changes might be necessary. Ronny wrote:

I agree, feature creep happened, problem is that with a surround for the collection, the errors can't be done sanely

We just finished some early design discussions about that last month and it will take a while to manifest

ptkenny commented 2 years ago

I released the PR anyway, if anything it might be a decent temporary fix.

danielgafni commented 1 year ago

Hey guys! Not sure what is the state of this issue, but the bug is still there:

>>> pytest.approx([{"a": 0.1}], rel=1e-1) == [{"a": 0.11}]
False
>>> pytest.approx(0.1, rel=1e-1) == 0.11
True

Pytest version is 7.2.0