FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ by coder-aditi · Pull Request #13543 · pytest-dev/pytest · GitHub

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

Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ #13543
nicoddemus merged 7 commits into
pytest-dev:mainfrom
coder-aditi:fix/decimal-approx-repr-code

Conversation

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 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM, thanks!

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

Copy link
Copy Markdown
Contributor Author

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

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

patchback Bot commented Jun 22, 2025
edited
Loading

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 <mysideuse@gmail.com>
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
(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 <92822822+coder-aditi@users.noreply.github.com>
Co-authored-by: AD <mysideuse@gmail.com>
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL