pytest-dev / pytest-dev/pytest

pytest.approx loses mismatch details for same-key mappings with unequal strings

Open
#15,009 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

pytest.approx supports non-numeric mapping values, but unequal strings under the same key cause its assertion diagnostic formatter to raise TypeError.

Save as test_approx_mapping.py:

import pytest


def test_mapping_string_mismatch():
    assert {"item": "a"} == pytest.approx({"item": "b"})

Run python -m pytest -q test_approx_mapping.py.

The assertion fails as expected, but its details are replaced by this message (local source path omitted):

(pytest_assertion plugin: representation of details failed: ... TypeError: unsupported operand type(s) for -: 'str' and 'str'.
 Probably an object has a faulty __repr__.)

Expected: a failed assertion with mapping mismatch details and no internal formatter exception. The equivalent unequal-string sequence retains its detail table.

#13010 and #13012 cover sequences; #13816 concerns different mapping keys. This example uses identical keys.

Reproduced on pytest 9.1.1 and development commit 3fd8675d6d798507c06cf9c60753be6d9d7b0e17, with CPython 3.12.3 on Linux 6.8.0-100-generic x86-64.

Installed packages (uv pip list --offline; this environment has no pip module):

Package          Version
---------------- -------
attrs            26.1.0
hypothesis       6.168.0
iniconfig        2.3.0
numpy            2.5.3
packaging        26.3
pluggy           1.6.0
pygments         2.21.0
pytest           9.1.1
sortedcontainers 2.4.0

GPT-6 Astra assisted; I manually reviewed this report.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Save the reproduction as test_approx_mapping.py and first run python -m pytest -q test_approx_mapping.py to observe the formatter failure. Trace pytest.approx mapping mismatch formatting, comparing it with the equivalent unequal-string sequence behavior. Done means the same-key mapping assertion reports mismatch details without an internal TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.