@@ -49,7 +49,7 @@ class FancyPrinter(PrettyPrinter):
49
49
# TODO: docs
50
50
_dispatch : MutableMapping [Callable , Callable ]
51
51
_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 ]
53
53
_dispatch = dict (PrettyPrinter ._dispatch ) # type: ignore
54
54
55
55
def _make_open (self , char : str , indent : int , obj ):
@@ -58,13 +58,13 @@ def _make_open(self, char: str, indent: int, obj):
58
58
else :
59
59
the_indent = ' ' * (indent + self ._indent_per_level )
60
60
61
- if len (obj ) and not self ._compact :
61
+ if len (obj ) and not self ._compact : # type: ignore
62
62
return f"{ char } \n { the_indent } "
63
63
else :
64
64
return char
65
65
66
66
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
68
68
return f",\n { ' ' * (indent + self ._indent_per_level )} { char } "
69
69
else :
70
70
return char
@@ -77,7 +77,14 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
77
77
write ((self ._indent_per_level - 1 ) * ' ' )
78
78
79
79
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
+ )
81
88
82
89
write (self ._make_close ('}' , indent , object ))
83
90
@@ -139,29 +146,23 @@ class ReprPrettyPrinter(FancyPrinter):
139
146
140
147
_dispatch = dict (FancyPrinter ._dispatch ) # type: ignore
141
148
142
- def pformat (self , object ):
149
+ def format_attributes (self , obj : Attributes ):
143
150
stream = StringIO ()
144
151
context = {}
145
152
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
154
154
155
155
stream .write (f"(\n { self ._indent_per_level * ' ' } " )
156
156
157
157
if self ._indent_per_level > 1 :
158
158
stream .write ((self ._indent_per_level - 1 ) * ' ' )
159
159
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 )
162
162
stream .write (f"\n { self ._indent_per_level * ' ' } )" )
163
163
164
- _dispatch [Attributes .__repr__ ] = _pprint_attributes
164
+ del context [id (obj )]
165
+ return stream .getvalue ()
165
166
166
167
def _format_attribute_items (self , items , stream , indent , allowance , context , level ):
167
168
@@ -174,14 +175,14 @@ def _format_attribute_items(self, items, stream, indent, allowance, context, lev
174
175
last = i == last_index
175
176
write (key )
176
177
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
+ )
185
186
186
187
if not last :
187
188
write (delimnl )
@@ -209,12 +210,12 @@ def __repr__(self) -> str:
209
210
210
211
class_name = f"{ type (self ).__module__ } .{ type (self ).__name__ } " if show_module else type (self ).__name__
211
212
212
- return f"{ class_name } { formatter .pformat (Attributes (self , * attributes ))} "
213
+ return f"{ class_name } { formatter .format_attributes (Attributes (self , * attributes ))} "
213
214
214
215
__repr__ .__doc__ = f"Return a string representation of the :class:`~{ obj .__module__ } .{ obj .__name__ } `."
215
216
__repr__ .__name__ = "__repr__"
216
217
__repr__ .__module__ = obj .__module__
217
- __repr__ .__qualname____ = f"{ obj .__module__ } .__repr__"
218
+ __repr__ .__qualname__ = f"{ obj .__module__ } .__repr__"
218
219
obj .__repr__ = __repr__
219
220
220
221
return obj
0 commit comments