Skip to content

Commit 767f08c

Browse files
pprint: Remove tracking of whether the object is recursive
This information is not used anywhere, we can simplify by just not tracking it
1 parent e5a448c commit 767f08c

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

src/_pytest/_io/pprint.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def _format(
114114
objid = id(object)
115115
if objid in context:
116116
stream.write(_recursion(object))
117-
self._recursive = True
118117
self._readable = False
119118
return
120119

@@ -487,21 +486,16 @@ def _format_items(
487486
write("\n" + " " * indent)
488487

489488
def _repr(self, object: Any, context: Dict[int, int], level: int) -> str:
490-
repr, readable, recursive = self.format(
491-
object, context.copy(), self._depth, level
492-
)
489+
repr, readable = self.format(object, context.copy(), self._depth, level)
493490
if not readable:
494491
self._readable = False
495-
if recursive:
496-
self._recursive = True
497492
return repr
498493

499494
def format(
500495
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
501-
) -> Tuple[str, bool, bool]:
496+
) -> Tuple[str, bool]:
502497
"""Format object for a specific context, returning a string
503-
and flags indicating whether the representation is 'readable'
504-
and whether the object represents a recursive construct.
498+
and a flag indicating whether the representation is 'readable'.
505499
"""
506500
return self._safe_repr(object, context, maxlevels, level)
507501

@@ -621,31 +615,30 @@ def _pprint_user_string(
621615

622616
def _safe_repr(
623617
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
624-
) -> Tuple[str, bool, bool]:
625-
# Return triple (repr_string, isreadable, isrecursive).
618+
) -> Tuple[str, bool]:
619+
# Return pair (repr_string, isreadable).
626620
typ = type(object)
627621
if typ in _builtin_scalars:
628-
return repr(object), True, False
622+
return repr(object), True
629623

630624
r = getattr(typ, "__repr__", None)
631625

632626
if issubclass(typ, int) and r is int.__repr__:
633627
if self._underscore_numbers:
634-
return f"{object:_d}", True, False
628+
return f"{object:_d}", True
635629
else:
636-
return repr(object), True, False
630+
return repr(object), True
637631

638632
if issubclass(typ, dict) and r is dict.__repr__:
639633
if not object:
640-
return "{}", True, False
634+
return "{}", True
641635
objid = id(object)
642636
if maxlevels and level >= maxlevels:
643-
return "{...}", False, objid in context
637+
return "{...}", False
644638
if objid in context:
645-
return _recursion(object), False, True
639+
return _recursion(object), False
646640
context[objid] = 1
647641
readable = True
648-
recursive = False
649642
components: List[str] = []
650643
append = components.append
651644
level += 1
@@ -654,51 +647,46 @@ def _safe_repr(
654647
else:
655648
items = object.items()
656649
for k, v in items:
657-
krepr, kreadable, krecur = self.format(k, context, maxlevels, level)
658-
vrepr, vreadable, vrecur = self.format(v, context, maxlevels, level)
650+
krepr, kreadable = self.format(k, context, maxlevels, level)
651+
vrepr, vreadable = self.format(v, context, maxlevels, level)
659652
append(f"{krepr}: {vrepr}")
660653
readable = readable and kreadable and vreadable
661-
if krecur or vrecur:
662-
recursive = True
663654
del context[objid]
664-
return "{%s}" % ", ".join(components), readable, recursive
655+
return "{%s}" % ", ".join(components), readable
665656

666657
if (issubclass(typ, list) and r is list.__repr__) or (
667658
issubclass(typ, tuple) and r is tuple.__repr__
668659
):
669660
if issubclass(typ, list):
670661
if not object:
671-
return "[]", True, False
662+
return "[]", True
672663
format = "[%s]"
673664
elif len(object) == 1:
674665
format = "(%s,)"
675666
else:
676667
if not object:
677-
return "()", True, False
668+
return "()", True
678669
format = "(%s)"
679670
objid = id(object)
680671
if maxlevels and level >= maxlevels:
681-
return format % "...", False, objid in context
672+
return format % "...", False
682673
if objid in context:
683-
return _recursion(object), False, True
674+
return _recursion(object), False
684675
context[objid] = 1
685676
readable = True
686-
recursive = False
687677
components = []
688678
append = components.append
689679
level += 1
690680
for o in object:
691-
orepr, oreadable, orecur = self.format(o, context, maxlevels, level)
681+
orepr, oreadable = self.format(o, context, maxlevels, level)
692682
append(orepr)
693683
if not oreadable:
694684
readable = False
695-
if orecur:
696-
recursive = True
697685
del context[objid]
698-
return format % ", ".join(components), readable, recursive
686+
return format % ", ".join(components), readable
699687

700688
rep = repr(object)
701-
return rep, bool(rep and not rep.startswith("<")), False
689+
return rep, bool(rep and not rep.startswith("<"))
702690

703691

704692
_builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)})

0 commit comments

Comments
 (0)