@@ -50,44 +50,40 @@ def collect_input_from_dict(
5050 base_model : type [BaseModel ], data : dict [str , Any ]
5151 ) -> dict [str , dict | Any ]:
5252 """
53- Recursively replace BaseModel objects with dictionary of 'data' values.
53+ Recursively replace BaseModel objects with nested dictionary of 'data' values.
5454
55- :param base_model: BaseModel object holding data and possibly other nested
56- BaseModel objects.
57- :param data: Dictionary of parameters and values without nesting structure.
55+ :param base_model: BaseModel object to structure data for.
56+ :param data: Flat dictionary of parameters and values without nesting structure.
5857 """
59- update = {}
58+ update = data . copy ()
6059 for field , info in base_model .model_fields .items ():
6160 if (
6261 isinstance (info .annotation , type )
6362 and not isinstance (info .annotation , GenericAlias )
6463 and issubclass (info .annotation , BaseModel )
6564 ):
66- update [field ] = BaseData .collect_input_from_dict (
67- info .annotation ,
68- data , # type: ignore
69- )
70- else :
71- if field in data :
72- update [field ] = data .get (field , info .default )
65+ # Nest and deal with aliases
66+ update = BaseData .collect_input_from_dict (info .annotation , update )
67+ nested = info .annotation .model_construct (** update )
68+ update [field ] = nested .model_dump (exclude_unset = True )
7369
7470 return update
7571
7672 @classmethod
77- def build (cls , input_data : InputFile | dict ) -> Self :
73+ def build (cls , input_data : InputFile | None = None , ** kwargs ) -> Self :
7874 """
7975 Build a dataclass from a dictionary or InputFile.
8076
8177 :param input_data: Dictionary of parameters and values.
8278
8379 :return: Dataclass of application parameters.
8480 """
85-
86- data = input_data
87-
81+ data = {}
8882 if isinstance (input_data , InputFile ) and input_data .data is not None :
8983 data = input_data .data .copy ()
9084
85+ data .update (kwargs )
86+
9187 if not isinstance (data , dict ):
9288 raise TypeError ("Input data must be a dictionary or InputFile." )
9389
0 commit comments