13530.bugfix.rst
Overview
This file documents a critical bug fix addressing a crash scenario encountered when using the `pytest.approx` function together with `decimal.Decimal` instances in Python, specifically when the `decimal.FloatOperation` trap is enabled. The fix ensures compatibility and stability of approximate numerical comparisons in testing environments that apply strict floating-point operation traps, thereby enhancing pytest's robustness when handling decimal arithmetic under strict conditions.
Purpose and Functionality
Problem Addressed:
The use ofpytest.approxwithdecimal.Decimalvalues caused crashes if thedecimal.FloatOperationtrap was set. This trap is designed to raise exceptions on implicit float-to-decimal conversions, which are sometimes necessary for approximate comparisons.Solution:
The fix modifies or safeguards the internal workings ofpytest.approxso that it properly interacts withdecimal.Decimalinstances without triggering theFloatOperationtrap, preventing the crash.Impact:
Test suites usingpytest.approxfor comparisons involving decimals can now run without failure even when strict decimal floating-point operation traps are enabled.
Details of the Fix
The file content itself is a brief changelog or bugfix note rather than source code. However, the underlying implementation likely involves:
Detecting when inputs to
pytest.approxare instances ofdecimal.Decimal.Avoiding operations that implicitly convert decimals to floats, or managing those conversions explicitly.
Possibly patching or adjusting internal numeric coercions in
pytest.approxto respect thedecimal.FloatOperationtrap.
This ensures that the approximate comparisons do not raise `FloatOperation` exceptions, which previously caused a crash.
Usage Example
import pytest
from decimal import Decimal, getcontext, FloatOperation
# Enable FloatOperation trap to raise errors on implicit float conversions
getcontext().traps[FloatOperation] = True
# Decimal values for comparison
a = Decimal('1.0')
b = Decimal('1.0001')
# Using pytest.approx to check approximate equality safely
assert a == pytest.approx(b, rel=1e-3)
Before this fix, the above code could crash due to the `FloatOperation` trap. After the fix, it executes without error, allowing safe approximate comparisons involving decimals.
Interaction with Other System Components
pytest Framework:
This fix is directly related to thepytest.approxutility, a core part of the pytest testing framework's approximate comparison feature.decimal Module:
It interacts with Python'sdecimalmodule, particularly with thedecimal.Decimalclass and the floating-point operation traps (decimal.FloatOperation).Testing Environments:
The fix enhances compatibility in test environments where strict decimal operation traps are enforced, such as financial or scientific applications requiring precise decimal arithmetic and error detection.Potential Integration:
The fix allows smoother integration of pytest in projects that enforce strict decimal operation policies, preventing unexpected crashes during automated testing.
Summary Diagram
Since this fix concerns the interaction between `pytest.approx` and `decimal.Decimal` under the `decimal.FloatOperation` trap, the following flowchart illustrates the logical flow of the approximate comparison process post-fix:
flowchart TD
A[Start: pytest.approx called] --> B{Input types?}
B -->|Both Decimal| C[Check FloatOperation trap]
B -->|Other types| F[Proceed with normal approx logic]
C -->|Trap enabled| D[Use safe decimal comparison methods]
C -->|Trap disabled| E[Use standard approx comparison]
D --> G[Return comparison result]
E --> G
F --> G
G --> H[End: Comparison result returned safely]
style C fill:#f9f,stroke:#333,stroke-width:1px
style D fill:#bbf,stroke:#333,stroke-width:1px
Explanation of Diagram:
When
pytest.approxis invoked, it first checks the input types.If both inputs are
decimal.Decimalinstances, it then checks if theFloatOperationtrap is enabled.If enabled, it uses specialized comparison logic that avoids triggering the trap.
Otherwise, it uses the standard approximate comparison logic.
For other input types, it proceeds with normal approximate comparison.
Finally, it returns the comparison result without causing a crash.
This fix strengthens `pytest.approx`'s handling of decimal arithmetic under strict floating-point operation traps, improving stability and reliability in numerical testing scenarios.