Skip to content

Approx type exposure #14164

@EatonEmmerich

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: 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