MCPcopy
hub / github.com/pytest-dev/pytest / _diff_text

Function _diff_text

src/_pytest/assertion/compare_text.py:41–86  ·  view source on GitHub ↗

Yield the explanation for the diff between text. Unless --verbose is used this will skip leading and trailing characters which are identical to keep the diff minimal.

(
    left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
)

Source from the content-addressed store, hash-verified

39
40
41def _diff_text(
42 left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
43) -> Iterator[str]:
44 """Yield the explanation for the diff between text.
45
46 Unless --verbose is used this will skip leading and trailing
47 characters which are identical to keep the diff minimal.
48 """
49 from difflib import ndiff
50
51 if verbose < 1:
52 i = 0 # just in case left or right has zero length
53 for i in range(min(len(left), len(right))):
54 if left[i] != right[i]:
55 break
56 if i > 42:
57 i -= 10 # Provide some context
58 yield f"Skipping {i} identical leading characters in diff, use -v to show"
59 left = left[i:]
60 right = right[i:]
61 if len(left) == len(right):
62 for i in range(len(left)):
63 if left[-i] != right[-i]:
64 break
65 if i > 42:
66 i -= 10 # Provide some context
67 yield (
68 f"Skipping {i} identical trailing "
69 "characters in diff, use -v to show"
70 )
71 left = left[:-i]
72 right = right[:-i]
73 keepends = True
74 if left.isspace() or right.isspace():
75 left = repr(str(left))
76 right = repr(str(right))
77 yield "Strings contain only whitespace, escaping them using repr()"
78 # "right" is the expected base against which we compare "left",
79 # see https://github.com/pytest-dev/pytest/issues/3333
80 yield from highlighter(
81 "\n".join(
82 line.strip("\n")
83 for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
84 ),
85 lexer="diff",
86 ).splitlines()
87
88
89def _notin_text(term: str, text: str, verbose: int = 0) -> Iterator[str]:

Callers 3

_check_matchMethod · 0.90
_compare_eq_textFunction · 0.85
_notin_textFunction · 0.85

Calls 2

joinMethod · 0.80
stripMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…