GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pytest.approx(Decimal(...)) Causes decimal.FloatOperation in __repr__ (pytest 8.4.0+) · Issue #13530 · pytest-dev/pytest

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

New issue
Copy link
New issue
Copy link
Closed
#13543
Closed
pytest.approx(Decimal(...)) Causes decimal.FloatOperation in __repr__ (pytest 8.4.0+)#13530
#13543
Copy link

Description

@rishid
rishid
opened on Jun 17, 2025
Issue body actions

pytest.approx() with decimal.Decimal objects triggers a decimal.FloatOperation error when the ApproxDecimal object's repr method is called (e.g., during print() or assertion formatting).

This happens because ApproxDecimal.repr compares its Decimal tolerance with hardcoded float literals (like 1e-3, 1e3). When the decimal context traps FloatOperations (to maintain Decimal precision), these implicit float-Decimal comparisons raise an error.

This issue started with pytest version 8.4.0 and seems related to changes from Issue #13010.

Pytest 8.4.0
Python 3.13.2
Ubuntu 24.04 LTS

# test_decimal_approx.py
import decimal

import pytest

decimal.getcontext().traps[decimal.FloatOperation] = True


def test_decimal_approx_repr_issue():
    """Reproduces the decimal.FloatOperation error from pytest.approx(Decimal)'s repr."""
    approx_obj = pytest.approx(decimal.Decimal("2.60"))
    print(f"Attempting to represent pytest.approx(Decimal): {approx_obj}")  # Triggers the bug
    assert decimal.Decimal("2.600001") == approx_obj


# You can run this test with:
# python -m pytest test_decimal_approx.py

Activity

  1. RonnyPfannschmidt commented on Jun 17, 2025

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Jun 17, 2025
    Member
    More actions

    Thanks for the details

    We where completely unaware

    This needs a unittests that triggers it locally

    I suspect it was missed before in the general error case and the referred change made it manifest

    Would you like to try your hand in contributing a gix as you seem to work with the details in more depth than we

  2. coder-aditi commented on Jun 18, 2025

    @coder-aditi
    coder-aditi
    on Jun 18, 2025
    Contributor
    More actions

    Hello @RonnyPfannschmidt , understanding the issue, I committed a fix, please review it and let me know your thoughts or any changes required.

  3. coder-aditi commented on Jun 18, 2025

    @coder-aditi
    coder-aditi
    on Jun 18, 2025
    Contributor
    More actions

    Hello @RonnyPfannschmidt , @nicoddemus , I made the necessary changes you guys suggested and committed in a new branch (was facing some branch conflicts, please have a look

    PR -> #13543

  4. nicoddemus
    closed this as completedin #13543on Jun 22, 2025
  5. nicoddemus
    added a commit that references this issue on Jun 22, 2025

    Fix `approx` usage with `decimal.FloatOperation` trap set (#13543)

    This issue will close once commit 111685c is merged into the 'main' branch.
    111685c
  6. patchback
    added a commit that references this issue on Jun 22, 2025

    Fix `approx` usage with `decimal.FloatOperation` trap set (#13543)

    This issue will close once commit f74629e is merged into the 'main' branch.
    f74629e
  7. nicoddemus
    added a commit that references this issue on Jun 22, 2025

    Fix `approx` usage with `decimal.FloatOperation` trap set (#13543) (#…

    This issue will close once commit 7b21af1 is merged into the 'main' branch.
    7b21af1
  8. luketainton
    added a commit that references this issue on Jan 5, 2026

    chore(deps): update dependency pytest to v9 (#305)

    4b75154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.