Skip to content

Commit 88c3546

Browse files
pprint: use a set instead of a dict for the context
This is really what the context is doing, we don't need to use a dict for it
1 parent 5060729 commit 88c3546

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/_pytest/_io/pprint.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from typing import Iterator
2525
from typing import List
2626
from typing import Optional
27+
from typing import Set
2728
from typing import Tuple
2829

2930

@@ -99,7 +100,7 @@ def __init__(
99100

100101
def pformat(self, object: Any) -> str:
101102
sio = _StringIO()
102-
self._format(object, sio, 0, 0, {}, 0)
103+
self._format(object, sio, 0, 0, set(), 0)
103104
return sio.getvalue()
104105

105106
def _format(
@@ -108,7 +109,7 @@ def _format(
108109
stream: IO[str],
109110
indent: int,
110111
allowance: int,
111-
context: Dict[int, int],
112+
context: Set[int],
112113
level: int,
113114
) -> None:
114115
objid = id(object)
@@ -118,9 +119,9 @@ def _format(
118119

119120
p = self._dispatch.get(type(object).__repr__, None)
120121
if p is not None:
121-
context[objid] = 1
122+
context.add(objid)
122123
p(self, object, stream, indent, allowance, context, level + 1)
123-
del context[objid]
124+
context.remove(objid)
124125
elif (
125126
_dataclasses.is_dataclass(object)
126127
and not isinstance(object, type)
@@ -130,11 +131,11 @@ def _format(
130131
hasattr(object.__repr__, "__wrapped__")
131132
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
132133
):
133-
context[objid] = 1
134+
context.add(objid)
134135
self._pprint_dataclass(
135136
object, stream, indent, allowance, context, level + 1
136137
)
137-
del context[objid]
138+
context.remove(objid)
138139
else:
139140
stream.write(self._repr(object, context, level))
140141

@@ -144,7 +145,7 @@ def _pprint_dataclass(
144145
stream: IO[str],
145146
indent: int,
146147
allowance: int,
147-
context: Dict[int, int],
148+
context: Set[int],
148149
level: int,
149150
) -> None:
150151
cls_name = object.__class__.__name__
@@ -159,7 +160,7 @@ def _pprint_dataclass(
159160

160161
_dispatch: Dict[
161162
Callable[..., str],
162-
Callable[["PrettyPrinter", Any, IO[str], int, int, Dict[int, int], int], None],
163+
Callable[["PrettyPrinter", Any, IO[str], int, int, Set[int], int], None],
163164
] = {}
164165

165166
def _pprint_dict(
@@ -168,7 +169,7 @@ def _pprint_dict(
168169
stream: IO[str],
169170
indent: int,
170171
allowance: int,
171-
context: Dict[int, int],
172+
context: Set[int],
172173
level: int,
173174
) -> None:
174175
write = stream.write
@@ -188,7 +189,7 @@ def _pprint_ordered_dict(
188189
stream: IO[str],
189190
indent: int,
190191
allowance: int,
191-
context: Dict[int, int],
192+
context: Set[int],
192193
level: int,
193194
) -> None:
194195
if not len(object):
@@ -207,7 +208,7 @@ def _pprint_list(
207208
stream: IO[str],
208209
indent: int,
209210
allowance: int,
210-
context: Dict[int, int],
211+
context: Set[int],
211212
level: int,
212213
) -> None:
213214
stream.write("[")
@@ -222,7 +223,7 @@ def _pprint_tuple(
222223
stream: IO[str],
223224
indent: int,
224225
allowance: int,
225-
context: Dict[int, int],
226+
context: Set[int],
226227
level: int,
227228
) -> None:
228229
stream.write("(")
@@ -237,7 +238,7 @@ def _pprint_set(
237238
stream: IO[str],
238239
indent: int,
239240
allowance: int,
240-
context: Dict[int, int],
241+
context: Set[int],
241242
level: int,
242243
) -> None:
243244
if not len(object):
@@ -263,7 +264,7 @@ def _pprint_str(
263264
stream: IO[str],
264265
indent: int,
265266
allowance: int,
266-
context: Dict[int, int],
267+
context: Set[int],
267268
level: int,
268269
) -> None:
269270
write = stream.write
@@ -322,7 +323,7 @@ def _pprint_bytes(
322323
stream: IO[str],
323324
indent: int,
324325
allowance: int,
325-
context: Dict[int, int],
326+
context: Set[int],
326327
level: int,
327328
) -> None:
328329
write = stream.write
@@ -351,7 +352,7 @@ def _pprint_bytearray(
351352
stream: IO[str],
352353
indent: int,
353354
allowance: int,
354-
context: Dict[int, int],
355+
context: Set[int],
355356
level: int,
356357
) -> None:
357358
write = stream.write
@@ -369,7 +370,7 @@ def _pprint_mappingproxy(
369370
stream: IO[str],
370371
indent: int,
371372
allowance: int,
372-
context: Dict[int, int],
373+
context: Set[int],
373374
level: int,
374375
) -> None:
375376
stream.write("mappingproxy(")
@@ -384,7 +385,7 @@ def _pprint_simplenamespace(
384385
stream: IO[str],
385386
indent: int,
386387
allowance: int,
387-
context: Dict[int, int],
388+
context: Set[int],
388389
level: int,
389390
) -> None:
390391
if type(object) is _types.SimpleNamespace:
@@ -406,7 +407,7 @@ def _format_dict_items(
406407
stream: IO[str],
407408
indent: int,
408409
allowance: int,
409-
context: Dict[int, int],
410+
context: Set[int],
410411
level: int,
411412
) -> None:
412413
if not items:
@@ -430,7 +431,7 @@ def _format_namespace_items(
430431
stream: IO[str],
431432
indent: int,
432433
allowance: int,
433-
context: Dict[int, int],
434+
context: Set[int],
434435
level: int,
435436
) -> None:
436437
if not items:
@@ -467,7 +468,7 @@ def _format_items(
467468
stream: IO[str],
468469
indent: int,
469470
allowance: int,
470-
context: Dict[int, int],
471+
context: Set[int],
471472
level: int,
472473
) -> None:
473474
if not items:
@@ -484,11 +485,11 @@ def _format_items(
484485

485486
write("\n" + " " * indent)
486487

487-
def _repr(self, object: Any, context: Dict[int, int], level: int) -> str:
488+
def _repr(self, object: Any, context: Set[int], level: int) -> str:
488489
return self.format(object, context.copy(), self._depth, level)
489490

490491
def format(
491-
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
492+
self, object: Any, context: Set[int], maxlevels: Optional[int], level: int
492493
) -> str:
493494
return self._safe_repr(object, context, maxlevels, level)
494495

@@ -498,7 +499,7 @@ def _pprint_default_dict(
498499
stream: IO[str],
499500
indent: int,
500501
allowance: int,
501-
context: Dict[int, int],
502+
context: Set[int],
502503
level: int,
503504
) -> None:
504505
rdf = self._repr(object.default_factory, context, level)
@@ -514,7 +515,7 @@ def _pprint_counter(
514515
stream: IO[str],
515516
indent: int,
516517
allowance: int,
517-
context: Dict[int, int],
518+
context: Set[int],
518519
level: int,
519520
) -> None:
520521
stream.write(object.__class__.__name__ + "(")
@@ -535,7 +536,7 @@ def _pprint_chain_map(
535536
stream: IO[str],
536537
indent: int,
537538
allowance: int,
538-
context: Dict[int, int],
539+
context: Set[int],
539540
level: int,
540541
) -> None:
541542
if not len(object.maps) or (len(object.maps) == 1 and not len(object.maps[0])):
@@ -554,7 +555,7 @@ def _pprint_deque(
554555
stream: IO[str],
555556
indent: int,
556557
allowance: int,
557-
context: Dict[int, int],
558+
context: Set[int],
558559
level: int,
559560
) -> None:
560561
stream.write(object.__class__.__name__ + "(")
@@ -573,7 +574,7 @@ def _pprint_user_dict(
573574
stream: IO[str],
574575
indent: int,
575576
allowance: int,
576-
context: Dict[int, int],
577+
context: Set[int],
577578
level: int,
578579
) -> None:
579580
self._format(object.data, stream, indent, allowance, context, level - 1)
@@ -586,7 +587,7 @@ def _pprint_user_list(
586587
stream: IO[str],
587588
indent: int,
588589
allowance: int,
589-
context: Dict[int, int],
590+
context: Set[int],
590591
level: int,
591592
) -> None:
592593
self._format(object.data, stream, indent, allowance, context, level - 1)
@@ -599,15 +600,15 @@ def _pprint_user_string(
599600
stream: IO[str],
600601
indent: int,
601602
allowance: int,
602-
context: Dict[int, int],
603+
context: Set[int],
603604
level: int,
604605
) -> None:
605606
self._format(object.data, stream, indent, allowance, context, level - 1)
606607

607608
_dispatch[_collections.UserString.__repr__] = _pprint_user_string
608609

609610
def _safe_repr(
610-
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
611+
self, object: Any, context: Set[int], maxlevels: Optional[int], level: int
611612
) -> str:
612613
typ = type(object)
613614
if typ in _builtin_scalars:
@@ -629,7 +630,7 @@ def _safe_repr(
629630
return "{...}"
630631
if objid in context:
631632
return _recursion(object)
632-
context[objid] = 1
633+
context.add(objid)
633634
components: List[str] = []
634635
append = components.append
635636
level += 1
@@ -641,7 +642,7 @@ def _safe_repr(
641642
krepr = self.format(k, context, maxlevels, level)
642643
vrepr = self.format(v, context, maxlevels, level)
643644
append(f"{krepr}: {vrepr}")
644-
del context[objid]
645+
context.remove(objid)
645646
return "{%s}" % ", ".join(components)
646647

647648
if (issubclass(typ, list) and r is list.__repr__) or (
@@ -662,14 +663,14 @@ def _safe_repr(
662663
return format % "..."
663664
if objid in context:
664665
return _recursion(object)
665-
context[objid] = 1
666+
context.add(objid)
666667
components = []
667668
append = components.append
668669
level += 1
669670
for o in object:
670671
orepr = self.format(o, context, maxlevels, level)
671672
append(orepr)
672-
del context[objid]
673+
context.remove(objid)
673674
return format % ", ".join(components)
674675

675676
return repr(object)

0 commit comments

Comments
 (0)