-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ #13543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ #13543
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Hi @nicoddemus please have a look on the PR once. Thank you |
for more information, see https://pre-commit.ci
Co-authored-by: Bruno Oliveira <[email protected]>
1f31c97
to
aa510b9
Compare
Backport to 8.4.x: 💚 backport PR created✅ Backport PR branch: Backported as #13555 🤖 @patchback |
Fixes #13530 --------- Co-authored-by: AD <[email protected]> Co-authored-by: Bruno Oliveira <[email protected]> (cherry picked from commit 111685c)
…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]>
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.