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

Function _saferepr

src/_pytest/assertion/rewrite.py:429–440  ·  view source on GitHub ↗

r"""Get a safe repr of an object for assertion error messages. The assertion formatting (util.format_explanation()) requires newlines to be escaped since they are a special character for it. Normally assertion.util.format_explanation() does this but for a custom repr it is possible

(obj: object)

Source from the content-addressed store, hash-verified

427
428
429def _saferepr(obj: object) -> str:
430 r"""Get a safe repr of an object for assertion error messages.
431
432 The assertion formatting (util.format_explanation()) requires
433 newlines to be escaped since they are a special character for it.
434 Normally assertion.util.format_explanation() does this but for a
435 custom repr it is possible to contain one of the special escape
436 sequences, especially '\n{' and '\n}' are likely to be present in
437 JSON reprs.
438 """
439 maxsize = _get_maxsize_for_saferepr(util._config)
440 return saferepr(obj, maxsize=maxsize).replace("\n", "\\n")
441
442
443def _get_maxsize_for_saferepr(config: Optional[Config]) -> Optional[int]:

Callers

nothing calls this directly

Calls 2

safereprFunction · 0.90

Tested by

no test coverage detected