Skip to content

pytest.approx gives confusing error when dictionaries have different keys #13816

@cmnemoi

Description

@cmnemoi

Hello,

While working on #12444, I used pytest.approx to compare two dictionaries with different keys.
The error message is pretty confusing. Instead of telling me that the keys don't match, I get an AssertionError with a representation issue and a KeyError for the missing key.

What I expected: A clearer message saying the dictionaries have different keys, something like: "comparison failed. dictionary has different keys: expected (a, c), got (a, b)".

What I got instead:

AssertionError: assert approx({'a': ... 4 ± 4.0e-06}) == {'a': 1, 'c': 3}

(pytest_assertion plugin: representation of details failed: /home/cmnemoi/code/repro_bugs/.venv/lib/python3.13/site-packages/_pytest/python_api.py:271: KeyError: 'b'.
 Probably an object has a faulty __repr__.)

How to reproduce

import pytest

def test_approx_dicts_with_mismatch_on_keys() -> None:
    expected = {"a": 1, "c": 3}
    actual = {"a": 1, "b": 4}

    assert pytest.approx(actual) == expected

Environment

  • pytest version: 8.4.2
  • Operating System: Pop_OS! 22.04 (GNU/Linux)

Installed Packages

Package                 Version   Editable project location
----------------------- --------- -----------------------------
annotated-types         0.7.0
certifi                 2025.10.5
charset-normalizer      3.4.4
click                   8.1.8
click-option-group      0.5.9
coverage                7.10.7
Deprecated              1.2.18
dotty-dict              1.3.1
filelock                3.20.0
gitdb                   4.0.12
GitPython               3.1.45
idna                    3.11
importlib_resources     6.5.2
iniconfig               2.1.0
Jinja2                  3.1.6
markdown-it-py          4.0.0
MarkupSafe              3.0.3
mdurl                   0.1.2
mypy                    1.18.2
mypy_extensions         1.1.0
packaging               25.0
pathspec                0.12.1
pip                     25.2
pluggy                  1.6.0
pydantic                2.12.2
pydantic_core           2.41.4
Pygments                2.19.2
pytest                  8.4.2
pytest-cov              7.0.0
pytest-mypy             1.0.1
pytest-watcher          0.4.3
python-gitlab           6.4.0
python-semantic-release 10.4.1
requests                2.32.5
requests-toolbelt       1.0.1
rich                    14.2.0
ruff                    0.14.0
shellingham             1.5.4
smmap                   5.0.2
tomlkit                 0.13.3
typing_extensions       4.15.0
typing-inspection       0.4.2
urllib3                 2.5.0
watchdog                6.0.0
wrapt                   1.17.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions