-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix decimal.FloatOperation error in pytest.approx Decimal __repr__ #13542
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
Changes from all commits
57e5d60
dae8cf9
4d0f1d6
95c5287
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,6 +530,17 @@ class ApproxDecimal(ApproxScalar): | |
DEFAULT_ABSOLUTE_TOLERANCE = Decimal("1e-12") | ||
DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6") | ||
|
||
def __repr__(self): | ||
rel = Decimal(str(self.rel)) if isinstance(self.rel, float) else self.rel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
abs_ = Decimal(str(self.abs)) if isinstance(self.abs, float) else self.abs | ||
if rel is not None and Decimal("1e-3") <= rel <= Decimal("1e3"): | ||
tol_str = f"{rel:.1e}" | ||
elif abs_ is not None: | ||
tol_str = f"{abs_:.1e}" | ||
else: | ||
tol_str = "???" | ||
return f"{self.expected} ± {tol_str}" | ||
|
||
|
||
def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: | ||
"""Assert that two numbers (or two ordered sequences of numbers) are equal to each other | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from __future__ import annotations | ||
|
||
import decimal | ||
|
||
import pytest | ||
|
||
|
||
def test_decimal_approx_repr_issue() -> None: | ||
trap = decimal.getcontext().traps[decimal.FloatOperation] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
decimal.getcontext().traps[decimal.FloatOperation] = False | ||
|
||
approx_obj = pytest.approx(decimal.Decimal("2.60")) | ||
print(f"Attempting to represent pytest.approx(Decimal): {approx_obj}") | ||
assert decimal.Decimal("2.600001") == approx_obj | ||
|
||
decimal.getcontext().traps[decimal.FloatOperation] = trap |
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.