@@ -181,30 +181,51 @@ def _format(self, object, stream, indent, allowance, context, level):
181181 self ._recursive = True
182182 self ._readable = False
183183 return
184+
185+ # If this is a subclass of PrettyPrinter, force all known
186+ # container types through their pretty-printers so that any
187+ # override of format() applies to their elements.
188+ if type (self ) is not PrettyPrinter :
189+ fn = type (object ).__repr__
190+ # skip raw repr for str/bytes
191+ if fn in self ._dispatch and not isinstance (object , (str , bytes , bytearray )):
192+ context [objid ] = 1
193+ self ._dispatch [fn ](
194+ self , object , stream ,
195+ indent , allowance , context , level + 1
196+ )
197+ del context [objid ]
198+ return
199+
200+ # fallback to one-line repr + width
184201 rep = self ._repr (object , context , level )
185202 max_width = self ._width - indent - allowance
186203 if len (rep ) > max_width :
187204 p = self ._dispatch .get (type (object ).__repr__ , None )
188- # Lazy import to improve module import time
189205 from dataclasses import is_dataclass
190206
191207 if p is not None :
192208 context [objid ] = 1
193- p (self , object , stream , indent , allowance , context , level + 1 )
209+ p (self , object , stream ,
210+ indent , allowance , context , level + 1 )
194211 del context [objid ]
195212 return
196- elif (is_dataclass (object ) and
197- not isinstance (object , type ) and
198- object .__dataclass_params__ .repr and
199- # Check dataclass has generated repr method.
200- hasattr (object .__repr__ , "__wrapped__" ) and
201- "__create_fn__" in object .__repr__ .__wrapped__ .__qualname__ ):
213+ elif (is_dataclass (object )
214+ and not isinstance (object , type )
215+ and object .__dataclass_params__ .repr
216+ and hasattr (object .__repr__ , "__wrapped__" )
217+ and "__create_fn__" in object .__repr__ .__wrapped__ .__qualname__ ):
202218 context [objid ] = 1
203- self ._pprint_dataclass (object , stream , indent , allowance , context , level + 1 )
219+ self ._pprint_dataclass (object , stream ,
220+ indent , allowance ,
221+ context , level + 1 )
204222 del context [objid ]
205223 return
224+
225+ # write the one-line repr
206226 stream .write (rep )
207227
228+
208229 def _pprint_dataclass (self , object , stream , indent , allowance , context , level ):
209230 # Lazy import to improve module import time
210231 from dataclasses import fields as dataclass_fields
0 commit comments