diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index 3f5c956d9cf..e81c781130c 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -3,6 +3,7 @@ from itertools import islice import pprint import reprlib +import sys def _try_repr_or_str(obj: object) -> str: @@ -112,6 +113,9 @@ def safeformat(obj: object) -> str: with a short exception info. """ try: + # Python 3.15+ supports expand=True for better nested reprs + if sys.version_info >= (3, 15): + return pprint.pformat(obj, expand=True) return pprint.pformat(obj) except Exception as exc: return _format_repr_exception(exc, obj)