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: Please improve repr readability · Issue #6985 · pytest-dev/pytest

pytest.approx: Please improve repr readability #6985

New issue
Copy link
New issue
Copy link
Closed
#12665
Closed
pytest.approx: Please improve repr readability#6985
#12665
Copy link
Labels
good first issueeasy issue that is friendly to new contributortopic: reportingrelated to terminal output and user-facing messages and errorstype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

Description

@henzef
henzef
opened on Mar 30, 2020
Issue body actions

It seems that pytest.approx repr is optimized for very high or very low tolerances, but is not as easily readable as it could be for values in between.
Some examples:

  • pytest.approx(42, abs=1) repr is 42 ± 1.0e+00, which could be just 42 ± 1.
  • pytest.approx(5, rel=0.01) repr is 5 ± 5.0e-02, but could be 5 ± 0.05 or maybe even 5 ± 1%
  • pytest.approx(24000, abs=500) repr is 24000 ± 5.0e+02 and could be 24000 ± 500
  • removing accuracy example: pytest.approx(1500, abs=555) repr is 1500 ± 5.6e+02 and could be 1500 ± 555

In these number ranges using the exact number instead of the exponential notation is both more accurate and easier readable for humans. Please adapt pytest.approx to switch it's repr style according to the input value ranges.

Activity

  1. hodevin commented on Mar 30, 2020

    @hodevin
    hodevin
    on Mar 30, 2020
    More actions

    Hey, my team and I would like to look over this issue and resolve it if that's okay.

    Reacted by Laurent Bristiel
  2. kalekundert commented on Mar 30, 2020

    @kalekundert
    kalekundert
    on Mar 30, 2020
    Contributor
    More actions

    Of course, PRs are always welcome! Hopefully this one won't be too hard, either.

  3. Zac-HD
    added
    topic: reportingrelated to terminal output and user-facing messages and errors
    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature
    on Mar 31, 2020
  4. Zac-HD commented on Mar 31, 2020

    @Zac-HD
    Zac-HD
    on Mar 31, 2020
    Member
    More actions

    See https://docs.python.org/3/library/string.html#format-specification-mini-language - you probably want to use the n format specifier.

    Reacted by Laurent Bristiel
  5. henzef commented on Apr 13, 2022

    @henzef
    henzef
    on Apr 13, 2022 · Hidden as spam
    Author
    show commentMore actions
  6. Zac-HD
    added
    good first issueeasy issue that is friendly to new contributor
    on Jul 6, 2024
  7. fazeelghafoor commented on Jul 28, 2024

    @fazeelghafoor
    fazeelghafoor
    on Jul 28, 2024
    Contributor
    More actions

    @Zac-HD is this issue still open, I would like to work on it

    Reacted by Zac Hatfield-Dodds
  8. nicoddemus
    closed this as completedin #12665on Aug 1, 2024
  9. nicoddemus
    closed this as completedin 5a01583on Aug 1, 2024
  10. HelloBroBro
    added a commit that references this issue on Aug 2, 2024

    Improve approx repr for better readability (pytest-dev#12665)

    This issue will close once commit 5a01583 is merged into the 'main' branch.
    5a01583
  11. rishid commented on Jun 17, 2025

    @rishid
    rishid
    on Jun 17, 2025
    More actions

    I think this change has a regression on line 409. I am catching a FloatOperation exception as this change is comparing a float to a Decimal.

    self = <[FloatOperation([<class 'decimal.FloatOperation'>]) raised in repr()] ApproxDecimal object at 0xffffa0403e00>
    
        def __repr__(self) -> str:
            """Return a string communicating both the expected value and the
            tolerance for the comparison being made.
    
            For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``.
            """
            # Don't show a tolerance for values that aren't compared using
            # tolerances, i.e. non-numerics and infinities. Need to call abs to
            # handle complex numbers, e.g. (inf + 1j).
            if (
                isinstance(self.expected, bool)
                or (not isinstance(self.expected, (Complex, Decimal)))
                or math.isinf(abs(self.expected) or isinstance(self.expected, bool))
            ):
                return str(self.expected)
    
            # If a sensible tolerance can't be calculated, self.tolerance will
            # raise a ValueError.  In this case, display '???'.
            try:
    >           if 1e-3 <= self.tolerance < 1e3: # <--- This is the exact line causing the FloatOperation
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    E           decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
    
    /usr/local/lib/python3.13/site-packages/_pytest/python_api.py:405: FloatOperation
  12. 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

    good first issueeasy issue that is friendly to new contributortopic: reportingrelated to terminal output and user-facing messages and errorstype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    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.