Skip to content

Fix decimal.FloatOperation error in pytest.approx Decimal __repr__#13542

Closed
coder-aditi wants to merge 4 commits intopytest-dev:mainfrom
coder-aditi:fix/decimal-approx-repr
Closed

Fix decimal.FloatOperation error in pytest.approx Decimal __repr__#13542
coder-aditi wants to merge 4 commits intopytest-dev:mainfrom
coder-aditi:fix/decimal-approx-repr

Conversation

@coder-aditi
Copy link
Contributor

@coder-aditi coder-aditi commented Jun 18, 2025

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.

Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

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

thanks for getting this started - this looks like a good start

i want to get some verification from people more familiar with Decimals and their behaviours



def test_decimal_approx_repr_issue():
trap = decimal.getcontext().traps[decimal.FloatOperation]
Copy link
Member

Choose a reason for hiding this comment

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

use momkeypatch.setitem to ensure undo happens even on failures (not that i expect a failure)

DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6")

def __repr__(self):
rel = Decimal(str(self.rel)) if isinstance(self.rel, float) else self.rel
Copy link
Member

Choose a reason for hiding this comment

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

we need to verify if using Decimal.from_float is more correct here

Copy link
Member

Choose a reason for hiding this comment

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

also we might need to generally ensure that rel is not a float

DEFAULT_ABSOLUTE_TOLERANCE = Decimal("1e-12")
DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6")

def __repr__(self):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def __repr__(self):
def __repr__(self) -> str:

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
@nicoddemus
Copy link
Member

Superseded by #13543.

@nicoddemus nicoddemus closed this Jun 22, 2025
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.

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

4 participants