Skip to content

Approx type exposure #14164

Closed
Closed

Description

@EatonEmmerich

What's the problem this feature will solve?

We are using pytest as a platform to execute qualification tests and have some documents generated from the output automatically. We therefore have a suite of tests with approx and/or numpy comparisons with tolerances.
This means we need to assert/log the differences between numpy arrays, which might be wrapped by pytest.approx.

Describe the solution you'd like

I would like to check whether the value compared is a value wrapped with approx and output the values in a specific way if it is.

  def unwrap_approx(a: np.ndarray) -> np.ndarray:
    if a.shape == () and isinstance(a[()], ApproxBase):
        return a[()].expected
    return a

  arr1 = np.array([1, 2, 3])
  arr2 = pytest.approx(np.array([1, 2, 4]))
  try:
    np.testing.assert_equal(arr1, arr2)
  except:
    print(f"Out of specification for test: %s, expected: %s", arr1,  unwrap_approx(arr2))

Alternative Solutions

I can use the ApproxBase type in _pytest.python_api in a isintance() check, and that works, but I would prefer if we didn't need to use the _pytest package as that is considered private.

from _pytest.python_api import ApproxBase
def unwrap_approx(a: np.ndarray) -> np.ndarray:
    if a.shape == () and isinstance(a[()], ApproxBase):
        return a[()].expected
    return a

Additional context

Activity

  1. RonnyPfannschmidt commented on Feb 5, 2026

    @RonnyPfannschmidt
    Member

    @bluetech i sense a need to bikeshedd what to epxpose for typing purposes

  2. bluetech commented on Jun 11, 2026

    @bluetech
    Member

    Yes, this is marked as a TODO in #7469. It is the return type of pytest.approx so I think it makes sense to expose it in some form.

    Some prerequisites for this (IMO):

    • Add _ispytest protection to the ctor to make it clear it is not meant to be instantiated. Alternatively, make it an ABC (probably a better idea, marking __repr__ and _yield_comparisons as @abstractmethod).

    • Improve the docstring for public exposure. Currently says "Provide shared utilities for making approximate comparisons between numbers or sequences of numbers". It would need to say something more like "The return value of pytest.approx()." and maybe a few words about its comparison behavior, or just say "see there for details".

    • Mark the attributes Final, I reckon none of them need to be mutable.

    • Decide which of the attributes should be public, and add _ prefix to the others:

      • expected
      • abs
      • rel
      • nan_ok

      These are just the stable parameters of pytest.approx() so I think it's fine to expose and document them.

    • I think we should think of a better name, maybe just Approx. The Base is an implementation detail, meaningless for a user of pytest.approx.

    Finally, exposing it means rexporting from src/pytest/__init__.py and adding to the API Reference. Patches welcome :)

  3. added 2 commits that reference this issue on Jun 23, 2026
    fa85a99
    913e586
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

    topic: approxrelated to pytest.approx functiontopic: typingtype-annotation issuetype: enhancementnew feature or API change, should be merged into features branch

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions