11from itertools import islice
22import rich
33
4+ from rich .repr import Result
45from typing_extensions import Self
56from typing import (
67 Any ,
@@ -42,21 +43,15 @@ def __getattr__(self, name):
4243
4344 def __str__ (self ) -> str :
4445 """Returns the string representation of the model."""
45- return self .model .__str__ ()
46+ return self ._model .__str__ ()
4647
4748 def __repr__ (self ) -> str :
48- """Returns a detailed string representation of the Item object, with each attribute on a new line."""
49- attrs = []
50- if hasattr (self .model .__class__ , "model_fields" ):
51- keys = self .model .__class__ .model_fields .keys ()
52- else :
53- keys = self .model .__dict__ .keys ()
54- for key in keys :
55- value = getattr (self .model , key , None )
56- attrs .append (f" { key } ={ value !r} " )
57- attrs_str = ",\n " .join (attrs )
58- return f"<{ self .__class__ .__name__ } \n { attrs_str } \n >"
59-
49+ """Returns the string representation of the model."""
50+ return self ._model .__repr__ ()
51+
52+ def __rich_repr__ (self ) -> Result :
53+ yield self ._model .__class__ .__name__ , self ._model .model_dump (exclude_defaults = True )
54+
6055 @property
6156 def pretty (self ):
6257 """Prints a pretty representation of the model using rich."""
@@ -132,18 +127,14 @@ def first(self) -> Model:
132127 if len (self ) == 0 :
133128 return None
134129 return self [0 ]
130+
131+ def __rich_repr__ (self ) -> Result :
132+ yield 'data' , list (self )
133+ yield 'pagination' , self ._pagination , None
134+ yield 'index' , self ._model .start_index if self ._model else 0 , 0
135+ yield 'count' , len (self )
135136
136- def __repr__ (self ) -> str :
137- if len (self ) == 0 :
138- return f"<{ self .__class__ .__name__ } (no items)>"
139-
140- parts = []
141- for item in islice (self , 10 ):
142- attrs = repr (item ).split (",\n " )
143- parts .append (",\n " .join (attrs [:10 ]) + "\n ..." )
144-
145- if len (self ) > 10 :
146- parts .append (" ..." )
147-
148- items = '\n ' .join (parts )
149- return f"<{ self .__class__ .__name__ } items=[\n { items } \n ]>"
137+ @property
138+ def pretty (self ):
139+ """Prints a pretty representation of the model using rich."""
140+ rich .print (self )
0 commit comments