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

Function assertrepr_compare

src/_pytest/assertion/util.py:138–175  ·  view source on GitHub ↗

Return specialised explanations for some operators/operands.

(config, op: str, left: Any, right: Any)

Source from the content-addressed store, hash-verified

136
137
138def assertrepr_compare(config, op: str, left: Any, right: Any) -> Optional[List[str]]:
139 """Return specialised explanations for some operators/operands."""
140 verbose = config.getoption("verbose")
141 if verbose > 1:
142 left_repr = safeformat(left)
143 right_repr = safeformat(right)
144 else:
145 # XXX: "15 chars indentation" is wrong
146 # ("E AssertionError: assert "); should use term width.
147 maxsize = (
148 80 - 15 - len(op) - 2
149 ) // 2 # 15 chars indentation, 1 space around op
150 left_repr = saferepr(left, maxsize=maxsize)
151 right_repr = saferepr(right, maxsize=maxsize)
152
153 summary = f"{left_repr} {op} {right_repr}"
154
155 explanation = None
156 try:
157 if op == "==":
158 explanation = _compare_eq_any(left, right, verbose)
159 elif op == "not in":
160 if istext(left) and istext(right):
161 explanation = _notin_text(left, right, verbose)
162 except outcomes.Exit:
163 raise
164 except Exception:
165 explanation = [
166 "(pytest_assertion plugin: representation of details failed: {}.".format(
167 _pytest._code.ExceptionInfo.from_current()._getreprcrash()
168 ),
169 " Probably an object has a faulty __repr__.)",
170 ]
171
172 if not explanation:
173 return None
174
175 return [summary] + explanation
176
177
178def _compare_eq_any(left: Any, right: Any, verbose: int = 0) -> List[str]:

Callers

nothing calls this directly

Calls 9

safeformatFunction · 0.90
safereprFunction · 0.90
_compare_eq_anyFunction · 0.85
istextFunction · 0.85
_notin_textFunction · 0.85
_getreprcrashMethod · 0.80
from_currentMethod · 0.80
getoptionMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected