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

Function _diff_text

src/_pytest/assertion/util.py:215–260  ·  view source on GitHub ↗

Return 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, verbose: int = 0)

Source from the content-addressed store, hash-verified

213
214
215def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
216 """Return the explanation for the diff between text.
217
218 Unless --verbose is used this will skip leading and trailing
219 characters which are identical to keep the diff minimal.
220 """
221 from difflib import ndiff
222
223 explanation: List[str] = []
224
225 if verbose < 1:
226 i = 0 # just in case left or right has zero length
227 for i in range(min(len(left), len(right))):
228 if left[i] != right[i]:
229 break
230 if i > 42:
231 i -= 10 # Provide some context
232 explanation = [
233 "Skipping %s identical leading characters in diff, use -v to show" % i
234 ]
235 left = left[i:]
236 right = right[i:]
237 if len(left) == len(right):
238 for i in range(len(left)):
239 if left[-i] != right[-i]:
240 break
241 if i > 42:
242 i -= 10 # Provide some context
243 explanation += [
244 "Skipping {} identical trailing "
245 "characters in diff, use -v to show".format(i)
246 ]
247 left = left[:-i]
248 right = right[:-i]
249 keepends = True
250 if left.isspace() or right.isspace():
251 left = repr(str(left))
252 right = repr(str(right))
253 explanation += ["Strings contain only whitespace, escaping them using repr()"]
254 # "right" is the expected base against which we compare "left",
255 # see https://github.com/pytest-dev/pytest/issues/3333
256 explanation += [
257 line.strip("\n")
258 for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
259 ]
260 return explanation
261
262
263def _compare_eq_verbose(left: Any, right: Any) -> List[str]:

Callers 2

_compare_eq_anyFunction · 0.85
_notin_textFunction · 0.85

Calls 2

stripMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected