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

Method _remove_unwanted_precision

src/_pytest/doctest.py:645–667  ·  view source on GitHub ↗
(self, want: str, got: str)

Source from the content-addressed store, hash-verified

643 return super().check_output(want, got, optionflags)
644
645 def _remove_unwanted_precision(self, want: str, got: str) -> str:
646 wants = list(self._number_re.finditer(want))
647 gots = list(self._number_re.finditer(got))
648 if len(wants) != len(gots):
649 return got
650 offset = 0
651 for w, g in zip(wants, gots):
652 fraction: Optional[str] = w.group("fraction")
653 exponent: Optional[str] = w.group("exponent1")
654 if exponent is None:
655 exponent = w.group("exponent2")
656 precision = 0 if fraction is None else len(fraction)
657 if exponent is not None:
658 precision -= int(exponent)
659 if float(w.group()) == approx(float(g.group()), abs=10 ** -precision):
660 # They're close enough. Replace the text we actually
661 # got with the text we want, so that it will match when we
662 # check the string literally.
663 got = (
664 got[: g.start() + offset] + w.group() + got[g.end() + offset :]
665 )
666 offset += w.end() - w.start() - (g.end() - g.start())
667 return got
668
669 return LiteralsOutputChecker
670

Callers 1

check_outputMethod · 0.95

Calls 4

approxFunction · 0.90
intClass · 0.85
endMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected