@@ -590,6 +590,9 @@ def get_plugin_classes(namespace: types.ModuleType, class_name: str) -> dict[str
590
590
def task_def_as_dict (
591
591
task_def : "type[Task]" ,
592
592
filter : ty .Callable [[attrs .Attribute , ty .Any ], bool ] | None = None ,
593
+ value_serializer : (
594
+ ty .Callable [[ty .Any , attrs .Attribute , ty .Any ], ty .Any ] | None
595
+ ) = None ,
593
596
** kwargs : ty .Any ,
594
597
) -> ty .Dict [str , ty .Any ]:
595
598
"""Converts a Pydra task class into a dictionary representation that can be serialized
@@ -604,9 +607,14 @@ def task_def_as_dict(
604
607
A function to filter out certain attributes from the task definition passed
605
608
through to `attrs.asdict`. It should take an attribute and its value as
606
609
arguments and return a boolean (True to keep the attribute, False to filter it out).
610
+ value_serializer : callable, optional
611
+ A function to serialize the value of an attribute. It should take the task
612
+ definition, the attribute, and its value as arguments and return a serialized
613
+ value.
607
614
**kwargs : dict
608
- Additional keyword arguments to pass to `attrs.asdict` (i.e. all except `filter`)
609
- See the `attrs` documentation for more details.
615
+ Additional keyword arguments to pass to `attrs.asdict` (i.e. all except `filter`,
616
+ and `value_serializer`), e.g. `recurse`. See the `attrs` documentation for more
617
+ details.
610
618
611
619
Returns
612
620
-------
@@ -621,15 +629,15 @@ def task_def_as_dict(
621
629
input_fields = task_fields (task_def )
622
630
executor = input_fields .pop (task_def ._executor_name ).default
623
631
input_dicts = [
624
- attrs .asdict (i , filter = filter , ** kwargs )
632
+ attrs .asdict (i , filter = filter , value_serializer = value_serializer , ** kwargs )
625
633
for i in input_fields
626
634
if (
627
635
not isinstance (i , Out ) # filter out outarg fields
628
636
and i .name not in task_def .BASE_ATTRS
629
637
)
630
638
]
631
639
output_dicts = [
632
- attrs .asdict (o , filter = filter , ** kwargs )
640
+ attrs .asdict (o , filter = filter , value_serializer = value_serializer , ** kwargs )
633
641
for o in task_fields (task_def .Outputs )
634
642
if o .name not in task_def .Outputs .BASE_ATTRS
635
643
]
@@ -640,7 +648,14 @@ def task_def_as_dict(
640
648
"inputs" : {d .pop ("name" ): d for d in input_dicts },
641
649
"outputs" : {d .pop ("name" ): d for d in output_dicts },
642
650
}
643
- dct .update ({a : getattr (task_def , "_" + a ) for a in task_def .TASK_CLASS_ATTRS })
651
+ class_attrs = {a : getattr (task_def , "_" + a ) for a in task_def .TASK_CLASS_ATTRS }
652
+ if value_serializer :
653
+ attrs_fields = {f .name : f for f in attrs .fields (task_def )}
654
+ class_attrs = {
655
+ n : value_serializer (task_def , attrs_fields [n ], v )
656
+ for n , v in class_attrs .items ()
657
+ }
658
+ dct .update (class_attrs )
644
659
645
660
return dct
646
661
0 commit comments