@@ -280,10 +280,10 @@ def simplify_combine_recipes(
280
280
281
281
return combined
282
282
283
- version : str = None
283
+ version : Optional [ str ] = None
284
284
args : RecipeArgs = Field (default_factory = RecipeArgs )
285
285
stages : List [RecipeStage ] = Field (default_factory = list )
286
- metadata : RecipeMetaData = None
286
+ metadata : Optional [ RecipeMetaData ] = None
287
287
args_evaluated : RecipeArgs = Field (default_factory = RecipeArgs )
288
288
289
289
def calculate_start (self ) -> int :
@@ -399,11 +399,12 @@ def remap_stages(cls, values: Dict[str, Any]) -> Dict[str, Any]:
399
399
formatted_values ["stages" ] = stages
400
400
401
401
# fill out any default argument values
402
- args = {}
402
+ args = {** values . pop ( "args" , {}) }
403
403
for key , val in values .items ():
404
- args [key ] = val
404
+ # avoid nesting the args in the recipe
405
+ if key not in cls .__pydantic_fields__ :
406
+ args [key ] = val
405
407
formatted_values ["args" ] = RecipeArgs (args )
406
-
407
408
return formatted_values
408
409
409
410
@staticmethod
@@ -504,7 +505,7 @@ def combine_metadata(self, metadata: Optional[RecipeMetaData]):
504
505
else :
505
506
self .metadata .update_missing_metadata (metadata )
506
507
507
- def dict (self , * args , ** kwargs ) -> Dict [str , Any ]:
508
+ def model_dump (self , * args , ** kwargs ) -> Dict [str , Any ]:
508
509
"""
509
510
:return: A dictionary representation of the recipe
510
511
"""
@@ -522,80 +523,17 @@ def dict(self, *args, **kwargs) -> Dict[str, Any]:
522
523
523
524
dict_ ["stages" ] = stages
524
525
525
- return dict_
526
-
527
- def model_dump (self , * args , ** kwargs ) -> Dict [str , Any ]:
528
- """
529
- Override the model_dump method to provide a dictionary representation that
530
- is compatible with model_validate.
531
-
532
- Unlike the standard model_dump, this transforms the stages list to a format
533
- expected by the validation logic, ensuring round-trip compatibility with
534
- model_validate.
535
-
536
- :return: A dictionary representation of the recipe compatible with
537
- model_validate
538
- """
539
- # Get the base dictionary from parent class
540
- base_dict = super ().model_dump (* args , ** kwargs )
541
-
542
- # Transform stages into the expected format
543
- if "stages" in base_dict :
544
- stages_dict = {}
545
- for stage in base_dict ["stages" ]:
546
- group = stage ["group" ]
547
- if group not in stages_dict :
548
- stages_dict [group ] = []
549
- stages_dict [group ].append (stage )
550
- base_dict ["stages" ] = stages_dict
551
-
552
- return base_dict
553
-
554
- def yaml (self , file_path : Optional [str ] = None ) -> str :
555
- """
556
- Return a yaml string representation of the recipe.
557
-
558
- :param file_path: optional file path to save yaml to
559
- :return: The yaml string representation of the recipe
560
- """
561
- file_stream = None if file_path is None else open (file_path , "w" )
562
- yaml_dict = self ._get_yaml_dict ()
563
-
564
- ret = yaml .dump (
565
- yaml_dict ,
566
- stream = file_stream ,
567
- allow_unicode = True ,
568
- sort_keys = False ,
569
- default_flow_style = None ,
570
- width = 88 ,
571
- )
572
-
573
- if file_stream is not None :
574
- file_stream .close ()
575
-
576
- return ret
577
-
578
- def _get_yaml_dict (self ) -> Dict [str , Any ]:
579
- """
580
- Get a dictionary representation of the recipe for yaml serialization
581
- The returned dict will only contain information necessary for yaml
582
- serialization and must not be used in place of the dict method
583
-
584
- :return: A dictionary representation of the recipe for yaml serialization
585
- """
586
-
587
- original_recipe_dict = self .dict ()
588
526
yaml_recipe_dict = {}
589
527
590
528
# populate recipe level attributes
591
529
recipe_level_attributes = ["version" , "args" , "metadata" ]
592
530
593
531
for attribute in recipe_level_attributes :
594
- if attribute_value := original_recipe_dict .get (attribute ):
532
+ if attribute_value := dict_ .get (attribute ):
595
533
yaml_recipe_dict [attribute ] = attribute_value
596
534
597
535
# populate stages
598
- stages = original_recipe_dict [ "stages" ]
536
+ stages = dict_ . pop ( "stages" , {})
599
537
for stage_name , stage_list in stages .items ():
600
538
for idx , stage in enumerate (stage_list ):
601
539
if len (stage_list ) > 1 :
@@ -616,6 +554,29 @@ def _get_yaml_dict(self) -> Dict[str, Any]:
616
554
617
555
return yaml_recipe_dict
618
556
557
+ def yaml (self , file_path : Optional [str ] = None ) -> str :
558
+ """
559
+ Return a yaml string representation of the recipe.
560
+
561
+ :param file_path: optional file path to save yaml to
562
+ :return: The yaml string representation of the recipe
563
+ """
564
+ file_stream = None if file_path is None else open (file_path , "w" )
565
+
566
+ ret = yaml .dump (
567
+ self .model_dump (),
568
+ stream = file_stream ,
569
+ allow_unicode = True ,
570
+ sort_keys = False ,
571
+ default_flow_style = None ,
572
+ width = 88 ,
573
+ )
574
+
575
+ if file_stream is not None :
576
+ file_stream .close ()
577
+
578
+ return ret
579
+
619
580
620
581
RecipeInput = Union [str , List [str ], Recipe , List [Recipe ], Modifier , List [Modifier ]]
621
582
RecipeStageInput = Union [str , List [str ], List [List [str ]]]
0 commit comments