Skip to content

pytest.approx considers boolean numeric types #9353

Closed
@The-Compiler

Description

@The-Compiler

From the docs:

You can also use approx to compare nonnumeric types, or dicts and sequences containing nonnumeric types, in which case it falls back to strict equality. This can be useful for comparing dicts and sequences that can contain optional values:

>>> {"required": 1.0000005, "optional": None} == approx({"required": 1, "optional": None})
True
>>> [None, 1.0000005] == approx([None,1])
True
>>> ["foo", 1.0000005] == approx([None,1])
False

However, that seems to treat booleans as numerical values too:

import pytest

def test_approx():
    d = {"value": 0.3, "active": True}
    expected = {"value": 0.1 + 0.2, "active": False}
    assert d == pytest.approx(expected)

results in:

AssertionError: assert {'active': True, 'value': 0.3} == approx({'value': 0.30000000000000004 ± 3.0e-07, 'active': False ± 1.0e-12})

(on Python 3.9.7 and pytest 6.2.5). Note the False ± 1.0e-12. While this is technically correct, it doesn't seem like a very reasonable way of comparing booleans.

cc @jvansanten who seems to have implemented this originally in #7710.

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