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

Function _format_assertmsg

src/_pytest/assertion/rewrite.py:453–472  ·  view source on GitHub ↗

r"""Format the custom assertion message given. For strings this simply replaces newlines with '\n~' so that util.format_explanation() will preserve them instead of escaping newlines. For other objects saferepr() is used first.

(obj: object)

Source from the content-addressed store, hash-verified

451
452
453def _format_assertmsg(obj: object) -> str:
454 r"""Format the custom assertion message given.
455
456 For strings this simply replaces newlines with '\n~' so that
457 util.format_explanation() will preserve them instead of escaping
458 newlines. For other objects saferepr() is used first.
459 """
460 # reprlib appears to have a bug which means that if a string
461 # contains a newline it gets escaped, however if an object has a
462 # .__repr__() which contains newlines it does not get escaped.
463 # However in either case we want to preserve the newline.
464 replaces = [("\n", "\n~"), ("%", "%%")]
465 if not isinstance(obj, str):
466 obj = saferepr(obj)
467 replaces.append(("\\n", "\n~"))
468
469 for r1, r2 in replaces:
470 obj = obj.replace(r1, r2)
471
472 return obj
473
474
475def _should_repr_global_name(obj: object) -> bool:

Callers

nothing calls this directly

Calls 2

safereprFunction · 0.90
appendMethod · 0.80

Tested by

no test coverage detected