Skip to content

Commit 19934b2

Browse files
Merge the AlwaysDispathPrettyPrinter into the now vendored PrettyPrinter
We don't need to keep the separation anymore, and this will make it easier to extend
1 parent 2953120 commit 19934b2

File tree

5 files changed

+34
-84
lines changed

5 files changed

+34
-84
lines changed

src/_pytest/_io/pprint.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,28 @@ def _format(self, object, stream, indent, allowance, context, level):
121121
self._recursive = True
122122
self._readable = False
123123
return
124-
rep = self._repr(object, context, level)
125-
max_width = self._width - indent - allowance
126-
if len(rep) > max_width:
127-
p = self._dispatch.get(type(object).__repr__, None)
128-
if p is not None:
129-
context[objid] = 1
130-
p(self, object, stream, indent, allowance, context, level + 1)
131-
del context[objid]
132-
return
133-
elif (
134-
_dataclasses.is_dataclass(object)
135-
and not isinstance(object, type)
136-
and object.__dataclass_params__.repr
137-
and
138-
# Check dataclass has generated repr method.
139-
hasattr(object.__repr__, "__wrapped__")
140-
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
141-
):
142-
context[objid] = 1
143-
self._pprint_dataclass(
144-
object, stream, indent, allowance, context, level + 1
145-
)
146-
del context[objid]
147-
return
148-
stream.write(rep)
124+
125+
p = self._dispatch.get(type(object).__repr__, None)
126+
if p is not None:
127+
context[objid] = 1
128+
p(self, object, stream, indent, allowance, context, level + 1)
129+
del context[objid]
130+
elif (
131+
_dataclasses.is_dataclass(object)
132+
and not isinstance(object, type)
133+
and object.__dataclass_params__.repr
134+
and
135+
# Check dataclass has generated repr method.
136+
hasattr(object.__repr__, "__wrapped__")
137+
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
138+
):
139+
context[objid] = 1
140+
self._pprint_dataclass(
141+
object, stream, indent, allowance, context, level + 1
142+
)
143+
del context[objid]
144+
else:
145+
stream.write(self._repr(object, context, level))
149146

150147
def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
151148
cls_name = object.__class__.__name__

src/_pytest/_io/saferepr.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import pprint
22
import reprlib
3-
from typing import Any
4-
from typing import Dict
5-
from typing import IO
63
from typing import Optional
74

8-
from .pprint import PrettyPrinter
9-
105

116
def _try_repr_or_str(obj: object) -> str:
127
try:
@@ -134,47 +129,3 @@ def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
134129
return repr(obj)
135130
except Exception as exc:
136131
return _format_repr_exception(exc, obj)
137-
138-
139-
class AlwaysDispatchingPrettyPrinter(PrettyPrinter):
140-
"""PrettyPrinter that always dispatches (regardless of width)."""
141-
142-
def _format(
143-
self,
144-
object: object,
145-
stream: IO[str],
146-
indent: int,
147-
allowance: int,
148-
context: Dict[int, Any],
149-
level: int,
150-
) -> None:
151-
p = self._dispatch.get(type(object).__repr__, None)
152-
153-
objid = id(object)
154-
if objid in context or p is None:
155-
super()._format(
156-
object,
157-
stream,
158-
indent,
159-
allowance,
160-
context,
161-
level,
162-
)
163-
return
164-
165-
context[objid] = 1
166-
p(self, object, stream, indent, allowance, context, level + 1)
167-
del context[objid]
168-
169-
170-
def _pformat_dispatch(
171-
object: object,
172-
indent: int = 1,
173-
width: int = 80,
174-
depth: Optional[int] = None,
175-
*,
176-
compact: bool = False,
177-
) -> str:
178-
return AlwaysDispatchingPrettyPrinter(
179-
indent=indent, width=width, depth=depth, compact=compact
180-
).pformat(object)

src/_pytest/assertion/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import _pytest._code
1818
from _pytest import outcomes
19-
from _pytest._io.saferepr import _pformat_dispatch
19+
from _pytest._io.pprint import PrettyPrinter
2020
from _pytest._io.saferepr import saferepr
2121
from _pytest._io.saferepr import saferepr_unlimited
2222
from _pytest.config import Config
@@ -348,8 +348,9 @@ def _compare_eq_iterable(
348348
lines_left = len(left_formatting)
349349
lines_right = len(right_formatting)
350350
if lines_left != lines_right:
351-
left_formatting = _pformat_dispatch(left).splitlines()
352-
right_formatting = _pformat_dispatch(right).splitlines()
351+
printer = PrettyPrinter()
352+
left_formatting = printer.pformat(left).splitlines()
353+
right_formatting = printer.pformat(right).splitlines()
353354

354355
if lines_left > 1 or lines_right > 1:
355356
_surrounding_parens_on_own_lines(left_formatting)

testing/io/test_pprint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from _pytest._io.pprint import PrettyPrinter
2+
3+
4+
def test_pformat_dispatch():
5+
printer = PrettyPrinter(width=5)
6+
assert printer.pformat("a") == "'a'"
7+
assert printer.pformat("a" * 10) == "'aaaaaaaaaa'"
8+
assert printer.pformat("foo bar") == "('foo '\n 'bar')"

testing/io/test_saferepr.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from _pytest._io.saferepr import _pformat_dispatch
32
from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
43
from _pytest._io.saferepr import saferepr
54
from _pytest._io.saferepr import saferepr_unlimited
@@ -159,12 +158,6 @@ def test_unicode():
159158
assert saferepr(val) == reprval
160159

161160

162-
def test_pformat_dispatch():
163-
assert _pformat_dispatch("a") == "'a'"
164-
assert _pformat_dispatch("a" * 10, width=5) == "'aaaaaaaaaa'"
165-
assert _pformat_dispatch("foo bar", width=5) == "('foo '\n 'bar')"
166-
167-
168161
def test_broken_getattribute():
169162
"""saferepr() can create proper representations of classes with
170163
broken __getattribute__ (#7145)

0 commit comments

Comments
 (0)