From c0b1a39192a998b4368ac859677b7e22f8ee56f2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 20 Nov 2019 17:56:39 +0100 Subject: [PATCH] minor: move internal _pformat_dispatch function --- src/_pytest/_io/saferepr.py | 21 +++++++++++++++++++++ src/_pytest/assertion/util.py | 22 +--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index 7fded872def..884f0a21ebd 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -80,3 +80,24 @@ def saferepr(obj: Any, maxsize: int = 240) -> str: around the Repr/reprlib functionality of the standard 2.6 lib. """ return SafeRepr(maxsize).repr(obj) + + +class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter): + """PrettyPrinter that always dispatches (regardless of width).""" + + def _format(self, object, stream, indent, allowance, context, level): + p = self._dispatch.get(type(object).__repr__, None) + + objid = id(object) + if objid in context or p is None: + return super()._format(object, stream, indent, allowance, context, level) + + context[objid] = 1 + p(self, object, stream, indent, allowance, context, level + 1) + del context[objid] + + +def _pformat_dispatch(object, indent=1, width=80, depth=None, *, compact=False): + return AlwaysDispatchingPrettyPrinter( + indent=1, width=80, depth=None, compact=False + ).pformat(object) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 4af35bd578d..67f8d46185e 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -13,6 +13,7 @@ import _pytest._code from _pytest import outcomes +from _pytest._io.saferepr import _pformat_dispatch from _pytest._io.saferepr import safeformat from _pytest._io.saferepr import saferepr from _pytest.compat import ATTRS_EQ_FIELD @@ -28,27 +29,6 @@ _assertion_pass = None # type: Optional[Callable[[int, str, str], None]] -class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter): - """PrettyPrinter that always dispatches (regardless of width).""" - - def _format(self, object, stream, indent, allowance, context, level): - p = self._dispatch.get(type(object).__repr__, None) - - objid = id(object) - if objid in context or p is None: - return super()._format(object, stream, indent, allowance, context, level) - - context[objid] = 1 - p(self, object, stream, indent, allowance, context, level + 1) - del context[objid] - - -def _pformat_dispatch(object, indent=1, width=80, depth=None, *, compact=False): - return AlwaysDispatchingPrettyPrinter( - indent=1, width=80, depth=None, compact=False - ).pformat(object) - - def format_explanation(explanation: str) -> str: """This formats an explanation