Skip to content

Commit 073a018

Browse files
committed
Update pretty_print type hints.
1 parent b8a5864 commit 073a018

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

domdf_python_tools/pretty_print.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FancyPrinter(PrettyPrinter):
4949
# TODO: docs
5050
_dispatch: MutableMapping[Callable, Callable]
5151
_indent_per_level: int
52-
_format_items: Callable[[Any, Any, Any, Any, Any, Any], None]
52+
_format_items: Callable[[PrettyPrinter, Any, Any, Any, Any, Any, Any], None]
5353
_dispatch = dict(PrettyPrinter._dispatch) # type: ignore
5454

5555
def _make_open(self, char: str, indent: int, obj):
@@ -58,13 +58,13 @@ def _make_open(self, char: str, indent: int, obj):
5858
else:
5959
the_indent = ' ' * (indent + self._indent_per_level)
6060

61-
if len(obj) and not self._compact:
61+
if len(obj) and not self._compact: # type: ignore
6262
return f"{char}\n{the_indent}"
6363
else:
6464
return char
6565

6666
def _make_close(self, char: str, indent: int, obj):
67-
if len(obj) and not self._compact:
67+
if len(obj) and not self._compact: # type: ignore
6868
return f",\n{' ' * (indent + self._indent_per_level)}{char}"
6969
else:
7070
return char
@@ -77,7 +77,14 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
7777
write((self._indent_per_level - 1) * ' ')
7878

7979
if len(object):
80-
self._format_dict_items(object.items(), stream, indent, allowance + 1, context, level)
80+
self._format_dict_items( # type: ignore
81+
object.items(),
82+
stream,
83+
indent,
84+
allowance + 1,
85+
context,
86+
level,
87+
)
8188

8289
write(self._make_close('}', indent, object))
8390

@@ -139,29 +146,23 @@ class ReprPrettyPrinter(FancyPrinter):
139146

140147
_dispatch = dict(FancyPrinter._dispatch) # type: ignore
141148

142-
def pformat(self, object):
149+
def format_attributes(self, obj: Attributes):
143150
stream = StringIO()
144151
context = {}
145152

146-
p = self._dispatch.get(type(object).__repr__, None)
147-
148-
context[id(object)] = 1
149-
p(self, object, stream, 0, 0, context, 1)
150-
del context[id(object)]
151-
return stream.getvalue()
152-
153-
def _pprint_attributes(self, object, stream, indent, allowance, context, level):
153+
context[id(obj)] = 1
154154

155155
stream.write(f"(\n{self._indent_per_level * ' '}")
156156

157157
if self._indent_per_level > 1:
158158
stream.write((self._indent_per_level - 1) * ' ')
159159

160-
if len(object):
161-
self._format_attribute_items(list(object), stream, indent, allowance + 1, context, level)
160+
if len(obj):
161+
self._format_attribute_items(list(obj), stream, 0, 0 + 1, context, 1)
162162
stream.write(f"\n{self._indent_per_level * ' '})")
163163

164-
_dispatch[Attributes.__repr__] = _pprint_attributes
164+
del context[id(obj)]
165+
return stream.getvalue()
165166

166167
def _format_attribute_items(self, items, stream, indent, allowance, context, level):
167168

@@ -174,14 +175,14 @@ def _format_attribute_items(self, items, stream, indent, allowance, context, lev
174175
last = i == last_index
175176
write(key)
176177
write('=')
177-
self._format(
178-
ent,
179-
stream,
180-
indent + len(key) + 2,
181-
allowance if last else 1,
182-
context,
183-
level,
184-
)
178+
self._format( # type: ignore
179+
ent,
180+
stream,
181+
indent + len(key) + 2,
182+
allowance if last else 1,
183+
context,
184+
level,
185+
)
185186

186187
if not last:
187188
write(delimnl)
@@ -209,12 +210,12 @@ def __repr__(self) -> str:
209210

210211
class_name = f"{type(self).__module__}.{type(self).__name__}" if show_module else type(self).__name__
211212

212-
return f"{class_name}{formatter.pformat(Attributes(self, *attributes))}"
213+
return f"{class_name}{formatter.format_attributes(Attributes(self, *attributes))}"
213214

214215
__repr__.__doc__ = f"Return a string representation of the :class:`~{obj.__module__}.{obj.__name__}`."
215216
__repr__.__name__ = "__repr__"
216217
__repr__.__module__ = obj.__module__
217-
__repr__.__qualname____ = f"{obj.__module__}.__repr__"
218+
__repr__.__qualname__ = f"{obj.__module__}.__repr__"
218219
obj.__repr__ = __repr__
219220

220221
return obj

0 commit comments

Comments
 (0)