Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error reporting in pytest.approx() #9520

Open
peffjepin opened this issue Jan 18, 2022 · 0 comments
Open

Error reporting in pytest.approx() #9520

peffjepin opened this issue Jan 18, 2022 · 0 comments
Labels
topic: approx Related to pytest.approx function type: bug problem that needs to be addressed

Comments

@peffjepin
Copy link

I've seen a couple test failures where pytest detects a relative tolerance < 0.

In the case I have a record of it was invoked like so:

def test_transform_base_case(transform_inputs):
    # assert that TransformComponent behaves in the same manner as Transform.
    regular = transforms.Transform(*transform_inputs)
    component = transforms.TransformComponent(*transform_inputs)
  
    vertex1 = np.array((1, 1, 1))
    vertex2 = np.array((1, 1, 1))
    regular.apply(vertex1)
    component.apply(vertex2)
  
    assert_approx(vertex1, vertex2)

Where assert_approx is defined as:

def assert_approx(iter1, iter2, rel=1e-6):
    for v1, v2 in zip(iter1, iter2):
        assert v1 == pytest.approx(v2, rel=rel)

Resulting in:

self = -9223372036854775808 ± ???
  
      @property
      def tolerance(self):
          """Return the tolerance for the comparison.
      
          This could be either an absolute tolerance or a relative tolerance,
          depending on what the user specified or which would be larger.
          """
      
          def set_default(x, default):
              return x if x is not None else default
      
          # Figure out what the absolute tolerance should be.  ``self.abs`` is
          # either None or a value specified by the user.
          absolute_tolerance = set_default(self.abs, self.DEFAULT_ABSOLUTE_TOLERANCE)
      
          if absolute_tolerance < 0:
              raise ValueError(
                  f"absolute tolerance can't be negative: {absolute_tolerance}"
              )
          if math.isnan(absolute_tolerance):
              raise ValueError("absolute tolerance can't be NaN.")
      
          # If the user specified an absolute tolerance but not a relative one,
          # just return the absolute tolerance.
          if self.rel is None:
              if self.abs is not None:
                  return absolute_tolerance
      
          # Figure out what the relative tolerance should be.  ``self.rel`` is
          # either None or a value specified by the user.  This is done after
          # we've made sure the user didn't ask for an absolute tolerance only,
          # because we don't want to raise errors about the relative tolerance if
          # we aren't even going to use it.
          relative_tolerance = set_default(
              self.rel, self.DEFAULT_RELATIVE_TOLERANCE
          ) * abs(self.expected)
      
          if relative_tolerance < 0:
  >           raise ValueError(
                  f"relative tolerance can't be negative: {absolute_tolerance}"
              )
  E           ValueError: relative tolerance can't be negative: 1e-12

I'm not familiar with the rest of the source for the class and can't reproduce the error with any consistency, but at the least it seems that

if relative_tolerance < 0:
    raise ValueError(
        f"relative tolerance can't be negative: {absolute_tolerance}"
    )

Should probably be:

if relative_tolerance < 0:
    raise ValueError(
        f"relative tolerance can't be negative: {relative_tolerance}"
    )
@Zac-HD Zac-HD added topic: approx Related to pytest.approx function type: bug problem that needs to be addressed labels Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: approx Related to pytest.approx function type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

2 participants