Skip to content

Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ - #13543

Merged
nicoddemus merged 7 commits into
pytest-dev:mainfrom
coder-aditi:fix/decimal-approx-repr-code
Jun 22, 2025
Merged

nicoddemus merged 7 commits into
pytest-dev:mainfrom
coder-aditi:fix/decimal-approx-repr-code

Conversation

@coder-aditi

Copy link
Copy Markdown
Contributor

Fix #13530

Summary

This PR addresses a decimal.FloatOperation error that happens when calling repr() on a pytest.approx object created with a decimal.Decimal value. The error arises from comparing and formatting Decimal and float values without proper conversion, which activates the FloatOperation trap.

Changes

I modified the repr method to carefully handle Decimal and float attributes. This is done by explicitly converting float values to Decimal before comparisons and formatting.
I added checks for None to prevent invalid comparisons.
I avoided direct comparisons between float and Decimal that can lead to FloatOperation exceptions.

Testing

I created a minimal test to reproduce the FloatOperation error from pytest.approx(Decimal)'s repr().
The test passes after applying this fix, confirming that the issue is resolved.

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment thread testing/python/approx.py Resolved
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 18, 2025
Comment thread testing/python/approx.py Resolved
@coder-aditi

Copy link
Copy Markdown
Contributor Author

Hi @nicoddemus please have a look on the PR once. Thank you

@nicoddemus
nicoddemus force-pushed the fix/decimal-approx-repr-code branch from 1f31c97 to aa510b9 Compare June 22, 2025 15:57
@nicoddemus
nicoddemus enabled auto-merge (squash) June 22, 2025 15:59
@nicoddemus
nicoddemus disabled auto-merge June 22, 2025 16:40
@nicoddemus
nicoddemus merged commit 111685c into pytest-dev:main Jun 22, 2025
@patchback

patchback Bot commented Jun 22, 2025 •

Copy link
Copy Markdown

Backport to 8.4.x: 💚 backport PR created

✅ Backport PR branch: patchback/backports/8.4.x/111685cc506fbaf87c105e67d68fbd881a47a84b/pr-13543

Backported as #13555

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback Bot pushed a commit that referenced this pull request Jun 22, 2025
Fixes #13530

---------

Co-authored-by: AD <[email protected]>
Co-authored-by: Bruno Oliveira <[email protected]>
(cherry picked from commit 111685c)
nicoddemus added a commit that referenced this pull request Jun 22, 2025
…13555)

Fixes #13530

---------



(cherry picked from commit 111685c)

Co-authored-by: Aditi De <[email protected]>
Co-authored-by: AD <[email protected]>
Co-authored-by: Bruno Oliveira <[email protected]>
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Sep 13, 2026
pytest-dev#13543 fixed the decimal.FloatOperation crash in __repr__ by overriding
it in ApproxDecimal so that self.tolerance was never touched. The
override copied ApproxScalar's `1e-3 <= self.tolerance < 1e3` bound,
where it only selects between plain and scientific formatting, and
reinterpreted it as a gate on whether to show `rel` at all. Two things
broke:

- the default tolerance, and any rel below 1e-3, rendered as "???";
- what was rendered was the raw `rel` ratio, not the absolute band that
  actually decides pass/fail, so approx(Decimal("2.60"), rel=Decimal("0.01"))
  claimed "± 1.0e-2" for a band of 0.026.

Drop the override and make ApproxScalar.__repr__ type-correct instead.
Decimal and float meet in exactly two places, and only one of them is
lossy: Decimal-to-float conversion saturates (float(Decimal("1e400")) is
inf, float(Decimal("1e-400")) is 0.0), while comparing a Decimal to a
float is exact and only signals FloatOperation. So the bound comparison
is skipped for Decimal and scientific notation used unconditionally --
which also keeps the output readable, since a tolerance that came from a
float carries its full binary expansion (abs=0.01 is 55 digits).

Removing the override re-exposed a bug it had been masking: for a NaN
Decimal, the negativity check in `tolerance` runs before the NaN check
and raises decimal.InvalidOperation rather than the ValueError that
__repr__ catches. Check for NaN first, matching the float path.

Every repr now matches pytest 8.3.5 again, except that Decimal("NaN")
degrades to "??? " instead of raising.

Co-Authored-By: Claude Opus 5 (1M context) via Claude Code <[email protected]>
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Sep 13, 2026
pytest-dev#13543 fixed the decimal.FloatOperation crash in __repr__ by overriding
it in ApproxDecimal so that self.tolerance was never touched. The
override copied ApproxScalar's `1e-3 <= self.tolerance < 1e3` bound,
where it only selects between plain and scientific formatting, and
reinterpreted it as a gate on whether to show `rel` at all. Two things
broke:

- the default tolerance, and any rel below 1e-3, rendered as "???";
- what was rendered was the raw `rel` ratio, not the absolute band that
  actually decides pass/fail, so approx(Decimal("2.60"), rel=Decimal("0.01"))
  claimed "± 1.0e-2" for a band of 0.026.

Drop the override and make ApproxScalar.__repr__ type-correct instead.
Decimal and float meet in exactly two places, and only one of them is
lossy: Decimal-to-float conversion saturates (float(Decimal("1e400")) is
inf, float(Decimal("1e-400")) is 0.0), while comparing a Decimal to a
float is exact and only signals FloatOperation. So the bound comparison
is skipped for Decimal and scientific notation used unconditionally --
which also keeps the output readable, since a tolerance that came from a
float carries its full binary expansion (abs=0.01 is 55 digits).

Removing the override re-exposed a bug it had been masking: for a NaN
Decimal, the negativity check in `tolerance` runs before the NaN check
and raises decimal.InvalidOperation rather than the ValueError that
__repr__ catches. Check for NaN first, matching the float path.

Every repr now matches pytest 8.3.5 again, except that Decimal("NaN")
degrades to "??? " instead of raising.

Co-Authored-By: Claude Opus 5 (1M context) via Claude Code <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytest.approx(Decimal(...)) Causes decimal.FloatOperation in __repr__ (pytest 8.4.0+)

3 participants