MCPcopy Create free account
hub / github.com/pytest-dev/pytest / __repr__

Method __repr__

src/_pytest/python_api.py:390–417  ·  view source on GitHub ↗

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°``.

(self)

Source from the content-addressed store, hash-verified

388 DEFAULT_RELATIVE_TOLERANCE: Union[float, Decimal] = 1e-6
389
390 def __repr__(self) -> str:
391 """Return a string communicating both the expected value and the
392 tolerance for the comparison being made.
393
394 For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``.
395 """
396 # Don't show a tolerance for values that aren't compared using
397 # tolerances, i.e. non-numerics and infinities. Need to call abs to
398 # handle complex numbers, e.g. (inf + 1j).
399 if (not isinstance(self.expected, (Complex, Decimal))) or math.isinf(
400 abs(self.expected) # type: ignore[arg-type]
401 ):
402 return str(self.expected)
403
404 # If a sensible tolerance can't be calculated, self.tolerance will
405 # raise a ValueError. In this case, display '???'.
406 try:
407 vetted_tolerance = f"{self.tolerance:.1e}"
408 if (
409 isinstance(self.expected, Complex)
410 and self.expected.imag
411 and not math.isinf(self.tolerance)
412 ):
413 vetted_tolerance += " ∠ ±180°"
414 except ValueError:
415 vetted_tolerance = "???"
416
417 return f"{self.expected} ± {vetted_tolerance}"
418
419 def __eq__(self, actual) -> bool:
420 """Return whether the given value is equal to the expected value

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected