pprint: Remove tracking of whether an object is readable

This information is not used anywhere
This commit is contained in:
Benjamin Schubert 2023-11-21 22:12:33 +00:00
parent 767f08cecd
commit 50607297f4
1 changed files with 19 additions and 33 deletions

View File

@ -114,7 +114,6 @@ class PrettyPrinter:
objid = id(object) objid = id(object)
if objid in context: if objid in context:
stream.write(_recursion(object)) stream.write(_recursion(object))
self._readable = False
return return
p = self._dispatch.get(type(object).__repr__, None) p = self._dispatch.get(type(object).__repr__, None)
@ -486,17 +485,11 @@ class PrettyPrinter:
write("\n" + " " * indent) write("\n" + " " * indent)
def _repr(self, object: Any, context: Dict[int, int], level: int) -> str: def _repr(self, object: Any, context: Dict[int, int], level: int) -> str:
repr, readable = self.format(object, context.copy(), self._depth, level) return self.format(object, context.copy(), self._depth, level)
if not readable:
self._readable = False
return repr
def format( def format(
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
) -> Tuple[str, bool]: ) -> str:
"""Format object for a specific context, returning a string
and a flag indicating whether the representation is 'readable'.
"""
return self._safe_repr(object, context, maxlevels, level) return self._safe_repr(object, context, maxlevels, level)
def _pprint_default_dict( def _pprint_default_dict(
@ -615,30 +608,28 @@ class PrettyPrinter:
def _safe_repr( def _safe_repr(
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
) -> Tuple[str, bool]: ) -> str:
# Return pair (repr_string, isreadable).
typ = type(object) typ = type(object)
if typ in _builtin_scalars: if typ in _builtin_scalars:
return repr(object), True return repr(object)
r = getattr(typ, "__repr__", None) r = getattr(typ, "__repr__", None)
if issubclass(typ, int) and r is int.__repr__: if issubclass(typ, int) and r is int.__repr__:
if self._underscore_numbers: if self._underscore_numbers:
return f"{object:_d}", True return f"{object:_d}"
else: else:
return repr(object), True return repr(object)
if issubclass(typ, dict) and r is dict.__repr__: if issubclass(typ, dict) and r is dict.__repr__:
if not object: if not object:
return "{}", True return "{}"
objid = id(object) objid = id(object)
if maxlevels and level >= maxlevels: if maxlevels and level >= maxlevels:
return "{...}", False return "{...}"
if objid in context: if objid in context:
return _recursion(object), False return _recursion(object)
context[objid] = 1 context[objid] = 1
readable = True
components: List[str] = [] components: List[str] = []
append = components.append append = components.append
level += 1 level += 1
@ -647,46 +638,41 @@ class PrettyPrinter:
else: else:
items = object.items() items = object.items()
for k, v in items: for k, v in items:
krepr, kreadable = self.format(k, context, maxlevels, level) krepr = self.format(k, context, maxlevels, level)
vrepr, vreadable = self.format(v, context, maxlevels, level) vrepr = self.format(v, context, maxlevels, level)
append(f"{krepr}: {vrepr}") append(f"{krepr}: {vrepr}")
readable = readable and kreadable and vreadable
del context[objid] del context[objid]
return "{%s}" % ", ".join(components), readable return "{%s}" % ", ".join(components)
if (issubclass(typ, list) and r is list.__repr__) or ( if (issubclass(typ, list) and r is list.__repr__) or (
issubclass(typ, tuple) and r is tuple.__repr__ issubclass(typ, tuple) and r is tuple.__repr__
): ):
if issubclass(typ, list): if issubclass(typ, list):
if not object: if not object:
return "[]", True return "[]"
format = "[%s]" format = "[%s]"
elif len(object) == 1: elif len(object) == 1:
format = "(%s,)" format = "(%s,)"
else: else:
if not object: if not object:
return "()", True return "()"
format = "(%s)" format = "(%s)"
objid = id(object) objid = id(object)
if maxlevels and level >= maxlevels: if maxlevels and level >= maxlevels:
return format % "...", False return format % "..."
if objid in context: if objid in context:
return _recursion(object), False return _recursion(object)
context[objid] = 1 context[objid] = 1
readable = True
components = [] components = []
append = components.append append = components.append
level += 1 level += 1
for o in object: for o in object:
orepr, oreadable = self.format(o, context, maxlevels, level) orepr = self.format(o, context, maxlevels, level)
append(orepr) append(orepr)
if not oreadable:
readable = False
del context[objid] del context[objid]
return format % ", ".join(components), readable return format % ", ".join(components)
rep = repr(object) return repr(object)
return rep, bool(rep and not rep.startswith("<"))
_builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)}) _builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)})