22
33from typing import Any , Final
44
5- from pydantic import BaseModel
5+ from pydantic import BaseModel , RootModel
66from rich .console import Console
77from rich .panel import Panel
88from rich .table import Table
@@ -64,11 +64,32 @@ def is_complex_change(old_val: Any, new_val: Any) -> bool:
6464 return False
6565
6666
67- def add_model_to_panel_content (model : BaseModel , indent : int = 0 ) -> list [str ]:
68- """Recursively build panel content line for a BaseModel."""
67+ def _add_root_model_to_panel_content (
68+ model : RootModel [Any ], indent : int = 0
69+ ) -> list [str ]:
70+ """Build and return panel content for a RootModel."""
71+ if not isinstance (model , RootModel ):
72+ raise ValueError (f"Received { type (model )} , but a RootModel is required" )
73+
6974 lines = []
7075 indent_str = " " * indent
76+ root_value = model .model_dump ()
77+ if isinstance (root_value , list ) and is_list_of_models (model .root ):
78+ for item in model .root :
79+ lines .append (f"{ indent_str } [dim]- { type (item ).__name__ } [/dim]" )
80+ lines .extend (add_model_to_panel_content (item , indent + 1 ))
81+ elif isinstance (root_value , dict ):
82+ for k , v in root_value .items ():
83+ lines .append (f"{ indent_str } [bold]{ k } :[/bold] { v } " )
84+ else :
85+ lines .append (f"{ indent_str } { root_value } " )
86+ return lines
87+
7188
89+ def _add_base_model_to_panel_content (model : BaseModel , indent : int = 0 ) -> list [str ]:
90+ """Build and return panel content for a BaseModel."""
91+ lines = []
92+ indent_str = " " * indent
7293 for key , value in model .model_dump ().items ():
7394 actual_value = getattr (model , key )
7495
@@ -91,10 +112,16 @@ def add_model_to_panel_content(model: BaseModel, indent: int = 0) -> list[str]:
91112 lines .append (f"{ indent_str } [dim]{ k } :[/dim] { v } " )
92113 else :
93114 lines .append (f"{ indent_str } [bold]{ key } :[/bold] { value } " )
94-
95115 return lines
96116
97117
118+ def add_model_to_panel_content (model : BaseModel , indent : int = 0 ) -> list [str ]:
119+ """Recursively build panel content line for a BaseModel."""
120+ if isinstance (model , RootModel ):
121+ return _add_root_model_to_panel_content (model , indent )
122+ return _add_base_model_to_panel_content (model , indent )
123+
124+
98125def render_basemodel_panel (
99126 model : BaseModel , field_path : str , added : bool = True
100127) -> Panel :
0 commit comments