util.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. """Utilities for assertion debugging."""
  2. import collections.abc
  3. import os
  4. import pprint
  5. from typing import AbstractSet
  6. from typing import Any
  7. from typing import Callable
  8. from typing import Iterable
  9. from typing import List
  10. from typing import Mapping
  11. from typing import Optional
  12. from typing import Sequence
  13. from unicodedata import normalize
  14. import _pytest._code
  15. from _pytest import outcomes
  16. from _pytest._io.saferepr import _pformat_dispatch
  17. from _pytest._io.saferepr import saferepr
  18. from _pytest._io.saferepr import saferepr_unlimited
  19. from _pytest.config import Config
  20. # The _reprcompare attribute on the util module is used by the new assertion
  21. # interpretation code and assertion rewriter to detect this plugin was
  22. # loaded and in turn call the hooks defined here as part of the
  23. # DebugInterpreter.
  24. _reprcompare: Optional[Callable[[str, object, object], Optional[str]]] = None
  25. # Works similarly as _reprcompare attribute. Is populated with the hook call
  26. # when pytest_runtest_setup is called.
  27. _assertion_pass: Optional[Callable[[int, str, str], None]] = None
  28. # Config object which is assigned during pytest_runtest_protocol.
  29. _config: Optional[Config] = None
  30. def format_explanation(explanation: str) -> str:
  31. r"""Format an explanation.
  32. Normally all embedded newlines are escaped, however there are
  33. three exceptions: \n{, \n} and \n~. The first two are intended
  34. cover nested explanations, see function and attribute explanations
  35. for examples (.visit_Call(), visit_Attribute()). The last one is
  36. for when one explanation needs to span multiple lines, e.g. when
  37. displaying diffs.
  38. """
  39. lines = _split_explanation(explanation)
  40. result = _format_lines(lines)
  41. return "\n".join(result)
  42. def _split_explanation(explanation: str) -> List[str]:
  43. r"""Return a list of individual lines in the explanation.
  44. This will return a list of lines split on '\n{', '\n}' and '\n~'.
  45. Any other newlines will be escaped and appear in the line as the
  46. literal '\n' characters.
  47. """
  48. raw_lines = (explanation or "").split("\n")
  49. lines = [raw_lines[0]]
  50. for values in raw_lines[1:]:
  51. if values and values[0] in ["{", "}", "~", ">"]:
  52. lines.append(values)
  53. else:
  54. lines[-1] += "\\n" + values
  55. return lines
  56. def _format_lines(lines: Sequence[str]) -> List[str]:
  57. """Format the individual lines.
  58. This will replace the '{', '}' and '~' characters of our mini formatting
  59. language with the proper 'where ...', 'and ...' and ' + ...' text, taking
  60. care of indentation along the way.
  61. Return a list of formatted lines.
  62. """
  63. result = list(lines[:1])
  64. stack = [0]
  65. stackcnt = [0]
  66. for line in lines[1:]:
  67. if line.startswith("{"):
  68. if stackcnt[-1]:
  69. s = "and "
  70. else:
  71. s = "where "
  72. stack.append(len(result))
  73. stackcnt[-1] += 1
  74. stackcnt.append(0)
  75. result.append(" +" + " " * (len(stack) - 1) + s + line[1:])
  76. elif line.startswith("}"):
  77. stack.pop()
  78. stackcnt.pop()
  79. result[stack[-1]] += line[1:]
  80. else:
  81. assert line[0] in ["~", ">"]
  82. stack[-1] += 1
  83. indent = len(stack) if line.startswith("~") else len(stack) - 1
  84. result.append(" " * indent + line[1:])
  85. assert len(stack) == 1
  86. return result
  87. def issequence(x: Any) -> bool:
  88. return isinstance(x, collections.abc.Sequence) and not isinstance(x, str)
  89. def istext(x: Any) -> bool:
  90. return isinstance(x, str)
  91. def isdict(x: Any) -> bool:
  92. return isinstance(x, dict)
  93. def isset(x: Any) -> bool:
  94. return isinstance(x, (set, frozenset))
  95. def isnamedtuple(obj: Any) -> bool:
  96. return isinstance(obj, tuple) and getattr(obj, "_fields", None) is not None
  97. def isdatacls(obj: Any) -> bool:
  98. return getattr(obj, "__dataclass_fields__", None) is not None
  99. def isattrs(obj: Any) -> bool:
  100. return getattr(obj, "__attrs_attrs__", None) is not None
  101. def isiterable(obj: Any) -> bool:
  102. try:
  103. iter(obj)
  104. return not istext(obj)
  105. except Exception:
  106. return False
  107. def has_default_eq(
  108. obj: object,
  109. ) -> bool:
  110. """Check if an instance of an object contains the default eq
  111. First, we check if the object's __eq__ attribute has __code__,
  112. if so, we check the equally of the method code filename (__code__.co_filename)
  113. to the default one generated by the dataclass and attr module
  114. for dataclasses the default co_filename is <string>, for attrs class, the __eq__ should contain "attrs eq generated"
  115. """
  116. # inspired from https://github.com/willmcgugan/rich/blob/07d51ffc1aee6f16bd2e5a25b4e82850fb9ed778/rich/pretty.py#L68
  117. if hasattr(obj.__eq__, "__code__") and hasattr(obj.__eq__.__code__, "co_filename"):
  118. code_filename = obj.__eq__.__code__.co_filename
  119. if isattrs(obj):
  120. return "attrs generated eq" in code_filename
  121. return code_filename == "<string>" # data class
  122. return True
  123. def assertrepr_compare(
  124. config, op: str, left: Any, right: Any, use_ascii: bool = False
  125. ) -> Optional[List[str]]:
  126. """Return specialised explanations for some operators/operands."""
  127. verbose = config.getoption("verbose")
  128. # Strings which normalize equal are often hard to distinguish when printed; use ascii() to make this easier.
  129. # See issue #3246.
  130. use_ascii = (
  131. isinstance(left, str)
  132. and isinstance(right, str)
  133. and normalize("NFD", left) == normalize("NFD", right)
  134. )
  135. if verbose > 1:
  136. left_repr = saferepr_unlimited(left, use_ascii=use_ascii)
  137. right_repr = saferepr_unlimited(right, use_ascii=use_ascii)
  138. else:
  139. # XXX: "15 chars indentation" is wrong
  140. # ("E AssertionError: assert "); should use term width.
  141. maxsize = (
  142. 80 - 15 - len(op) - 2
  143. ) // 2 # 15 chars indentation, 1 space around op
  144. left_repr = saferepr(left, maxsize=maxsize, use_ascii=use_ascii)
  145. right_repr = saferepr(right, maxsize=maxsize, use_ascii=use_ascii)
  146. summary = f"{left_repr} {op} {right_repr}"
  147. explanation = None
  148. try:
  149. if op == "==":
  150. explanation = _compare_eq_any(left, right, verbose)
  151. elif op == "not in":
  152. if istext(left) and istext(right):
  153. explanation = _notin_text(left, right, verbose)
  154. except outcomes.Exit:
  155. raise
  156. except Exception:
  157. explanation = [
  158. "(pytest_assertion plugin: representation of details failed: {}.".format(
  159. _pytest._code.ExceptionInfo.from_current()._getreprcrash()
  160. ),
  161. " Probably an object has a faulty __repr__.)",
  162. ]
  163. if not explanation:
  164. return None
  165. return [summary] + explanation
  166. def _compare_eq_any(left: Any, right: Any, verbose: int = 0) -> List[str]:
  167. explanation = []
  168. if istext(left) and istext(right):
  169. explanation = _diff_text(left, right, verbose)
  170. else:
  171. from _pytest.python_api import ApproxBase
  172. if isinstance(left, ApproxBase) or isinstance(right, ApproxBase):
  173. # Although the common order should be obtained == expected, this ensures both ways
  174. approx_side = left if isinstance(left, ApproxBase) else right
  175. other_side = right if isinstance(left, ApproxBase) else left
  176. explanation = approx_side._repr_compare(other_side)
  177. elif type(left) == type(right) and (
  178. isdatacls(left) or isattrs(left) or isnamedtuple(left)
  179. ):
  180. # Note: unlike dataclasses/attrs, namedtuples compare only the
  181. # field values, not the type or field names. But this branch
  182. # intentionally only handles the same-type case, which was often
  183. # used in older code bases before dataclasses/attrs were available.
  184. explanation = _compare_eq_cls(left, right, verbose)
  185. elif issequence(left) and issequence(right):
  186. explanation = _compare_eq_sequence(left, right, verbose)
  187. elif isset(left) and isset(right):
  188. explanation = _compare_eq_set(left, right, verbose)
  189. elif isdict(left) and isdict(right):
  190. explanation = _compare_eq_dict(left, right, verbose)
  191. if isiterable(left) and isiterable(right):
  192. expl = _compare_eq_iterable(left, right, verbose)
  193. explanation.extend(expl)
  194. return explanation
  195. def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
  196. """Return the explanation for the diff between text.
  197. Unless --verbose is used this will skip leading and trailing
  198. characters which are identical to keep the diff minimal.
  199. """
  200. from difflib import ndiff
  201. explanation: List[str] = []
  202. if verbose < 1:
  203. i = 0 # just in case left or right has zero length
  204. for i in range(min(len(left), len(right))):
  205. if left[i] != right[i]:
  206. break
  207. if i > 42:
  208. i -= 10 # Provide some context
  209. explanation = [
  210. "Skipping %s identical leading characters in diff, use -v to show" % i
  211. ]
  212. left = left[i:]
  213. right = right[i:]
  214. if len(left) == len(right):
  215. for i in range(len(left)):
  216. if left[-i] != right[-i]:
  217. break
  218. if i > 42:
  219. i -= 10 # Provide some context
  220. explanation += [
  221. "Skipping {} identical trailing "
  222. "characters in diff, use -v to show".format(i)
  223. ]
  224. left = left[:-i]
  225. right = right[:-i]
  226. keepends = True
  227. if left.isspace() or right.isspace():
  228. left = repr(str(left))
  229. right = repr(str(right))
  230. explanation += ["Strings contain only whitespace, escaping them using repr()"]
  231. # "right" is the expected base against which we compare "left",
  232. # see https://github.com/pytest-dev/pytest/issues/3333
  233. explanation += [
  234. line.strip("\n")
  235. for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
  236. ]
  237. return explanation
  238. def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
  239. """Move opening/closing parenthesis/bracket to own lines."""
  240. opening = lines[0][:1]
  241. if opening in ["(", "[", "{"]:
  242. lines[0] = " " + lines[0][1:]
  243. lines[:] = [opening] + lines
  244. closing = lines[-1][-1:]
  245. if closing in [")", "]", "}"]:
  246. lines[-1] = lines[-1][:-1] + ","
  247. lines[:] = lines + [closing]
  248. def _compare_eq_iterable(
  249. left: Iterable[Any], right: Iterable[Any], verbose: int = 0
  250. ) -> List[str]:
  251. if verbose <= 0 and not running_on_ci():
  252. return ["Use -v to get more diff"]
  253. # dynamic import to speedup pytest
  254. import difflib
  255. left_formatting = pprint.pformat(left).splitlines()
  256. right_formatting = pprint.pformat(right).splitlines()
  257. # Re-format for different output lengths.
  258. lines_left = len(left_formatting)
  259. lines_right = len(right_formatting)
  260. if lines_left != lines_right:
  261. left_formatting = _pformat_dispatch(left).splitlines()
  262. right_formatting = _pformat_dispatch(right).splitlines()
  263. if lines_left > 1 or lines_right > 1:
  264. _surrounding_parens_on_own_lines(left_formatting)
  265. _surrounding_parens_on_own_lines(right_formatting)
  266. explanation = ["Full diff:"]
  267. # "right" is the expected base against which we compare "left",
  268. # see https://github.com/pytest-dev/pytest/issues/3333
  269. explanation.extend(
  270. line.rstrip() for line in difflib.ndiff(right_formatting, left_formatting)
  271. )
  272. return explanation
  273. def _compare_eq_sequence(
  274. left: Sequence[Any], right: Sequence[Any], verbose: int = 0
  275. ) -> List[str]:
  276. comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes)
  277. explanation: List[str] = []
  278. len_left = len(left)
  279. len_right = len(right)
  280. for i in range(min(len_left, len_right)):
  281. if left[i] != right[i]:
  282. if comparing_bytes:
  283. # when comparing bytes, we want to see their ascii representation
  284. # instead of their numeric values (#5260)
  285. # using a slice gives us the ascii representation:
  286. # >>> s = b'foo'
  287. # >>> s[0]
  288. # 102
  289. # >>> s[0:1]
  290. # b'f'
  291. left_value = left[i : i + 1]
  292. right_value = right[i : i + 1]
  293. else:
  294. left_value = left[i]
  295. right_value = right[i]
  296. explanation += [f"At index {i} diff: {left_value!r} != {right_value!r}"]
  297. break
  298. if comparing_bytes:
  299. # when comparing bytes, it doesn't help to show the "sides contain one or more
  300. # items" longer explanation, so skip it
  301. return explanation
  302. len_diff = len_left - len_right
  303. if len_diff:
  304. if len_diff > 0:
  305. dir_with_more = "Left"
  306. extra = saferepr(left[len_right])
  307. else:
  308. len_diff = 0 - len_diff
  309. dir_with_more = "Right"
  310. extra = saferepr(right[len_left])
  311. if len_diff == 1:
  312. explanation += [f"{dir_with_more} contains one more item: {extra}"]
  313. else:
  314. explanation += [
  315. "%s contains %d more items, first extra item: %s"
  316. % (dir_with_more, len_diff, extra)
  317. ]
  318. return explanation
  319. def _compare_eq_set(
  320. left: AbstractSet[Any], right: AbstractSet[Any], verbose: int = 0
  321. ) -> List[str]:
  322. explanation = []
  323. diff_left = left - right
  324. diff_right = right - left
  325. if diff_left:
  326. explanation.append("Extra items in the left set:")
  327. for item in diff_left:
  328. explanation.append(saferepr(item))
  329. if diff_right:
  330. explanation.append("Extra items in the right set:")
  331. for item in diff_right:
  332. explanation.append(saferepr(item))
  333. return explanation
  334. def _compare_eq_dict(
  335. left: Mapping[Any, Any], right: Mapping[Any, Any], verbose: int = 0
  336. ) -> List[str]:
  337. explanation: List[str] = []
  338. set_left = set(left)
  339. set_right = set(right)
  340. common = set_left.intersection(set_right)
  341. same = {k: left[k] for k in common if left[k] == right[k]}
  342. if same and verbose < 2:
  343. explanation += ["Omitting %s identical items, use -vv to show" % len(same)]
  344. elif same:
  345. explanation += ["Common items:"]
  346. explanation += pprint.pformat(same).splitlines()
  347. diff = {k for k in common if left[k] != right[k]}
  348. if diff:
  349. explanation += ["Differing items:"]
  350. for k in diff:
  351. explanation += [saferepr({k: left[k]}) + " != " + saferepr({k: right[k]})]
  352. extra_left = set_left - set_right
  353. len_extra_left = len(extra_left)
  354. if len_extra_left:
  355. explanation.append(
  356. "Left contains %d more item%s:"
  357. % (len_extra_left, "" if len_extra_left == 1 else "s")
  358. )
  359. explanation.extend(
  360. pprint.pformat({k: left[k] for k in extra_left}).splitlines()
  361. )
  362. extra_right = set_right - set_left
  363. len_extra_right = len(extra_right)
  364. if len_extra_right:
  365. explanation.append(
  366. "Right contains %d more item%s:"
  367. % (len_extra_right, "" if len_extra_right == 1 else "s")
  368. )
  369. explanation.extend(
  370. pprint.pformat({k: right[k] for k in extra_right}).splitlines()
  371. )
  372. return explanation
  373. def _compare_eq_cls(left: Any, right: Any, verbose: int) -> List[str]:
  374. if not has_default_eq(left):
  375. return []
  376. if isdatacls(left):
  377. import dataclasses
  378. all_fields = dataclasses.fields(left)
  379. fields_to_check = [info.name for info in all_fields if info.compare]
  380. elif isattrs(left):
  381. all_fields = left.__attrs_attrs__
  382. fields_to_check = [field.name for field in all_fields if getattr(field, "eq")]
  383. elif isnamedtuple(left):
  384. fields_to_check = left._fields
  385. else:
  386. assert False
  387. indent = " "
  388. same = []
  389. diff = []
  390. for field in fields_to_check:
  391. if getattr(left, field) == getattr(right, field):
  392. same.append(field)
  393. else:
  394. diff.append(field)
  395. explanation = []
  396. if same or diff:
  397. explanation += [""]
  398. if same and verbose < 2:
  399. explanation.append("Omitting %s identical items, use -vv to show" % len(same))
  400. elif same:
  401. explanation += ["Matching attributes:"]
  402. explanation += pprint.pformat(same).splitlines()
  403. if diff:
  404. explanation += ["Differing attributes:"]
  405. explanation += pprint.pformat(diff).splitlines()
  406. for field in diff:
  407. field_left = getattr(left, field)
  408. field_right = getattr(right, field)
  409. explanation += [
  410. "",
  411. "Drill down into differing attribute %s:" % field,
  412. ("%s%s: %r != %r") % (indent, field, field_left, field_right),
  413. ]
  414. explanation += [
  415. indent + line
  416. for line in _compare_eq_any(field_left, field_right, verbose)
  417. ]
  418. return explanation
  419. def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
  420. index = text.find(term)
  421. head = text[:index]
  422. tail = text[index + len(term) :]
  423. correct_text = head + tail
  424. diff = _diff_text(text, correct_text, verbose)
  425. newdiff = ["%s is contained here:" % saferepr(term, maxsize=42)]
  426. for line in diff:
  427. if line.startswith("Skipping"):
  428. continue
  429. if line.startswith("- "):
  430. continue
  431. if line.startswith("+ "):
  432. newdiff.append(" " + line[2:])
  433. else:
  434. newdiff.append(line)
  435. return newdiff
  436. def running_on_ci() -> bool:
  437. """Check if we're currently running on a CI system."""
  438. env_vars = ["CI", "BUILD_NUMBER"]
  439. return any(var in os.environ for var in env_vars)