Skip to content

pytest.approx Raises misleading error for Sequences/Collections containing different strings #13010

@marcelotrevisani

Description

@marcelotrevisani

Hi folks,

I was using pytest.approx and I found a scenario when using approx in a collection or sequence that can have multiples types, the problem arise when doing:

def test_approx_str():
    assert [1.0, 2.0, "abc"] == approx([1.0, 2.0, "aaa"])

I was expecting pytest to bypass the approx for str type, but I am seeing the following error:

    def test_approx_str():
>       assert [1.0, 2.0, "abc"] == approx([1.0, 2.0, "aaa"])
E       AssertionError: assert [1.0, 2.0, 'abc'] == approx([1.0 ±...2.0e-06, aaa])
E         
E         (pytest_assertion plugin: representation of details failed: /tmp/test_a/.venv/lib/python3.12/site-packages/_pytest/python_api.py:343: TypeError: unsupported operand type(s) for -: 'str' and 'str'.
E          Probably an object has a faulty __repr__.)

test_foo.py:5: AssertionError

let me know if you need more info about this

Package   Version
--------- -------
iniconfig 2.0.0
packaging 24.2
pluggy    1.5.0
pytest    8.3.3
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: approxrelated to pytest.approx function

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions