24
24
from typing import Iterator
25
25
from typing import List
26
26
from typing import Optional
27
+ from typing import Set
27
28
from typing import Tuple
28
29
29
30
@@ -99,7 +100,7 @@ def __init__(
99
100
100
101
def pformat (self , object : Any ) -> str :
101
102
sio = _StringIO ()
102
- self ._format (object , sio , 0 , 0 , {} , 0 )
103
+ self ._format (object , sio , 0 , 0 , set () , 0 )
103
104
return sio .getvalue ()
104
105
105
106
def _format (
@@ -108,7 +109,7 @@ def _format(
108
109
stream : IO [str ],
109
110
indent : int ,
110
111
allowance : int ,
111
- context : Dict [ int , int ],
112
+ context : Set [ int ],
112
113
level : int ,
113
114
) -> None :
114
115
objid = id (object )
@@ -118,9 +119,9 @@ def _format(
118
119
119
120
p = self ._dispatch .get (type (object ).__repr__ , None )
120
121
if p is not None :
121
- context [ objid ] = 1
122
+ context . add ( objid )
122
123
p (self , object , stream , indent , allowance , context , level + 1 )
123
- del context [ objid ]
124
+ context . remove ( objid )
124
125
elif (
125
126
_dataclasses .is_dataclass (object )
126
127
and not isinstance (object , type )
@@ -130,11 +131,11 @@ def _format(
130
131
hasattr (object .__repr__ , "__wrapped__" )
131
132
and "__create_fn__" in object .__repr__ .__wrapped__ .__qualname__
132
133
):
133
- context [ objid ] = 1
134
+ context . add ( objid )
134
135
self ._pprint_dataclass (
135
136
object , stream , indent , allowance , context , level + 1
136
137
)
137
- del context [ objid ]
138
+ context . remove ( objid )
138
139
else :
139
140
stream .write (self ._repr (object , context , level ))
140
141
@@ -144,7 +145,7 @@ def _pprint_dataclass(
144
145
stream : IO [str ],
145
146
indent : int ,
146
147
allowance : int ,
147
- context : Dict [ int , int ],
148
+ context : Set [ int ],
148
149
level : int ,
149
150
) -> None :
150
151
cls_name = object .__class__ .__name__
@@ -159,7 +160,7 @@ def _pprint_dataclass(
159
160
160
161
_dispatch : Dict [
161
162
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 ],
163
164
] = {}
164
165
165
166
def _pprint_dict (
@@ -168,7 +169,7 @@ def _pprint_dict(
168
169
stream : IO [str ],
169
170
indent : int ,
170
171
allowance : int ,
171
- context : Dict [ int , int ],
172
+ context : Set [ int ],
172
173
level : int ,
173
174
) -> None :
174
175
write = stream .write
@@ -188,7 +189,7 @@ def _pprint_ordered_dict(
188
189
stream : IO [str ],
189
190
indent : int ,
190
191
allowance : int ,
191
- context : Dict [ int , int ],
192
+ context : Set [ int ],
192
193
level : int ,
193
194
) -> None :
194
195
if not len (object ):
@@ -207,7 +208,7 @@ def _pprint_list(
207
208
stream : IO [str ],
208
209
indent : int ,
209
210
allowance : int ,
210
- context : Dict [ int , int ],
211
+ context : Set [ int ],
211
212
level : int ,
212
213
) -> None :
213
214
stream .write ("[" )
@@ -222,7 +223,7 @@ def _pprint_tuple(
222
223
stream : IO [str ],
223
224
indent : int ,
224
225
allowance : int ,
225
- context : Dict [ int , int ],
226
+ context : Set [ int ],
226
227
level : int ,
227
228
) -> None :
228
229
stream .write ("(" )
@@ -237,7 +238,7 @@ def _pprint_set(
237
238
stream : IO [str ],
238
239
indent : int ,
239
240
allowance : int ,
240
- context : Dict [ int , int ],
241
+ context : Set [ int ],
241
242
level : int ,
242
243
) -> None :
243
244
if not len (object ):
@@ -263,7 +264,7 @@ def _pprint_str(
263
264
stream : IO [str ],
264
265
indent : int ,
265
266
allowance : int ,
266
- context : Dict [ int , int ],
267
+ context : Set [ int ],
267
268
level : int ,
268
269
) -> None :
269
270
write = stream .write
@@ -322,7 +323,7 @@ def _pprint_bytes(
322
323
stream : IO [str ],
323
324
indent : int ,
324
325
allowance : int ,
325
- context : Dict [ int , int ],
326
+ context : Set [ int ],
326
327
level : int ,
327
328
) -> None :
328
329
write = stream .write
@@ -351,7 +352,7 @@ def _pprint_bytearray(
351
352
stream : IO [str ],
352
353
indent : int ,
353
354
allowance : int ,
354
- context : Dict [ int , int ],
355
+ context : Set [ int ],
355
356
level : int ,
356
357
) -> None :
357
358
write = stream .write
@@ -369,7 +370,7 @@ def _pprint_mappingproxy(
369
370
stream : IO [str ],
370
371
indent : int ,
371
372
allowance : int ,
372
- context : Dict [ int , int ],
373
+ context : Set [ int ],
373
374
level : int ,
374
375
) -> None :
375
376
stream .write ("mappingproxy(" )
@@ -384,7 +385,7 @@ def _pprint_simplenamespace(
384
385
stream : IO [str ],
385
386
indent : int ,
386
387
allowance : int ,
387
- context : Dict [ int , int ],
388
+ context : Set [ int ],
388
389
level : int ,
389
390
) -> None :
390
391
if type (object ) is _types .SimpleNamespace :
@@ -406,7 +407,7 @@ def _format_dict_items(
406
407
stream : IO [str ],
407
408
indent : int ,
408
409
allowance : int ,
409
- context : Dict [ int , int ],
410
+ context : Set [ int ],
410
411
level : int ,
411
412
) -> None :
412
413
if not items :
@@ -430,7 +431,7 @@ def _format_namespace_items(
430
431
stream : IO [str ],
431
432
indent : int ,
432
433
allowance : int ,
433
- context : Dict [ int , int ],
434
+ context : Set [ int ],
434
435
level : int ,
435
436
) -> None :
436
437
if not items :
@@ -467,7 +468,7 @@ def _format_items(
467
468
stream : IO [str ],
468
469
indent : int ,
469
470
allowance : int ,
470
- context : Dict [ int , int ],
471
+ context : Set [ int ],
471
472
level : int ,
472
473
) -> None :
473
474
if not items :
@@ -484,11 +485,11 @@ def _format_items(
484
485
485
486
write ("\n " + " " * indent )
486
487
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 :
488
489
return self .format (object , context .copy (), self ._depth , level )
489
490
490
491
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
492
493
) -> str :
493
494
return self ._safe_repr (object , context , maxlevels , level )
494
495
@@ -498,7 +499,7 @@ def _pprint_default_dict(
498
499
stream : IO [str ],
499
500
indent : int ,
500
501
allowance : int ,
501
- context : Dict [ int , int ],
502
+ context : Set [ int ],
502
503
level : int ,
503
504
) -> None :
504
505
rdf = self ._repr (object .default_factory , context , level )
@@ -514,7 +515,7 @@ def _pprint_counter(
514
515
stream : IO [str ],
515
516
indent : int ,
516
517
allowance : int ,
517
- context : Dict [ int , int ],
518
+ context : Set [ int ],
518
519
level : int ,
519
520
) -> None :
520
521
stream .write (object .__class__ .__name__ + "(" )
@@ -535,7 +536,7 @@ def _pprint_chain_map(
535
536
stream : IO [str ],
536
537
indent : int ,
537
538
allowance : int ,
538
- context : Dict [ int , int ],
539
+ context : Set [ int ],
539
540
level : int ,
540
541
) -> None :
541
542
if not len (object .maps ) or (len (object .maps ) == 1 and not len (object .maps [0 ])):
@@ -554,7 +555,7 @@ def _pprint_deque(
554
555
stream : IO [str ],
555
556
indent : int ,
556
557
allowance : int ,
557
- context : Dict [ int , int ],
558
+ context : Set [ int ],
558
559
level : int ,
559
560
) -> None :
560
561
stream .write (object .__class__ .__name__ + "(" )
@@ -573,7 +574,7 @@ def _pprint_user_dict(
573
574
stream : IO [str ],
574
575
indent : int ,
575
576
allowance : int ,
576
- context : Dict [ int , int ],
577
+ context : Set [ int ],
577
578
level : int ,
578
579
) -> None :
579
580
self ._format (object .data , stream , indent , allowance , context , level - 1 )
@@ -586,7 +587,7 @@ def _pprint_user_list(
586
587
stream : IO [str ],
587
588
indent : int ,
588
589
allowance : int ,
589
- context : Dict [ int , int ],
590
+ context : Set [ int ],
590
591
level : int ,
591
592
) -> None :
592
593
self ._format (object .data , stream , indent , allowance , context , level - 1 )
@@ -599,15 +600,15 @@ def _pprint_user_string(
599
600
stream : IO [str ],
600
601
indent : int ,
601
602
allowance : int ,
602
- context : Dict [ int , int ],
603
+ context : Set [ int ],
603
604
level : int ,
604
605
) -> None :
605
606
self ._format (object .data , stream , indent , allowance , context , level - 1 )
606
607
607
608
_dispatch [_collections .UserString .__repr__ ] = _pprint_user_string
608
609
609
610
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
611
612
) -> str :
612
613
typ = type (object )
613
614
if typ in _builtin_scalars :
@@ -629,7 +630,7 @@ def _safe_repr(
629
630
return "{...}"
630
631
if objid in context :
631
632
return _recursion (object )
632
- context [ objid ] = 1
633
+ context . add ( objid )
633
634
components : List [str ] = []
634
635
append = components .append
635
636
level += 1
@@ -641,7 +642,7 @@ def _safe_repr(
641
642
krepr = self .format (k , context , maxlevels , level )
642
643
vrepr = self .format (v , context , maxlevels , level )
643
644
append (f"{ krepr } : { vrepr } " )
644
- del context [ objid ]
645
+ context . remove ( objid )
645
646
return "{%s}" % ", " .join (components )
646
647
647
648
if (issubclass (typ , list ) and r is list .__repr__ ) or (
@@ -662,14 +663,14 @@ def _safe_repr(
662
663
return format % "..."
663
664
if objid in context :
664
665
return _recursion (object )
665
- context [ objid ] = 1
666
+ context . add ( objid )
666
667
components = []
667
668
append = components .append
668
669
level += 1
669
670
for o in object :
670
671
orepr = self .format (o , context , maxlevels , level )
671
672
append (orepr )
672
- del context [ objid ]
673
+ context . remove ( objid )
673
674
return format % ", " .join (components )
674
675
675
676
return repr (object )
0 commit comments