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

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:

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

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:


This fix strengthens `pytest.approx`'s handling of decimal arithmetic under strict floating-point operation traps, improving stability and reliability in numerical testing scenarios.