Skip to content

[PR #13543/111685cc backport][8.4.x] Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ #13555

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

Conversation

patchback[bot]
Copy link

@patchback patchback bot commented Jun 22, 2025

This is a backport of PR #13543 as merged into main (111685c).

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.

Fixes #13530

---------

Co-authored-by: AD <mysideuse@gmail.com>
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
(cherry picked from commit 111685c)
@nicoddemus nicoddemus disabled auto-merge June 22, 2025 17:06
@nicoddemus nicoddemus merged commit 7b21af1 into 8.4.x Jun 22, 2025
35 of 36 checks passed
@nicoddemus nicoddemus deleted the patchback/backports/8.4.x/111685cc506fbaf87c105e67d68fbd881a47a84b/pr-13543 branch June 22, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants