3636 "make_task_spec" ,
3737]
3838
39- RESERVED_OUTPUT_NAMES = ("split" , "combine" )
40-
4139
4240class _Empty (enum .Enum ):
4341
@@ -58,6 +56,11 @@ def is_type(_, __, val: ty.Any) -> bool:
5856 return inspect .isclass (val ) or ty .get_origin (val )
5957
6058
59+ def convert_default_value (value : ty .Any , self_ : "Field" ) -> ty .Any :
60+ """Ensure the default value has been coerced into the correct type"""
61+ return TypeParser [self_ .type ](self_ .type , label = self_ .name )(value )
62+
63+
6164@attrs .define (kw_only = True )
6265class Field :
6366 """Base class for input and output fields to task specifications
@@ -66,9 +69,11 @@ class Field:
6669 ----------
6770 name: str, optional
6871 The name of the field, used when specifying a list of fields instead of a mapping
69- from name to field, by default it is None
7072 type: type, optional
7173 The type of the field, by default it is Any
74+ from name to field, by default it is None
75+ default : Any, optional
76+ the default value for the field, by default it is EMPTY
7277 help_string: str, optional
7378 A short description of the input field.
7479 requires: list, optional
@@ -83,6 +88,9 @@ class Field:
8388 type : ty .Type [ty .Any ] = attrs .field (
8489 validator = is_type , default = ty .Any , converter = default_if_none (ty .Any )
8590 )
91+ default : ty .Any = attrs .field (
92+ default = EMPTY , converter = attrs .Converter (convert_default_value , with_self = True )
93+ )
8694 help_string : str = ""
8795 requires : list [str ] | list [list [str ]] = attrs .field (
8896 factory = list , converter = ensure_list
@@ -97,10 +105,15 @@ class Arg(Field):
97105
98106 Parameters
99107 ----------
108+ name: str, optional
109+ The name of the field, used when specifying a list of fields instead of a mapping
110+ from name to field, by default it is None
111+ type: type, optional
112+ The type of the field, by default it is Any
113+ default : Any, optional
114+ the default value for the field, by default it is EMPTY
100115 help_string: str
101116 A short description of the input field.
102- default : Any, optional
103- the default value for the argument
104117 allowed_values: list, optional
105118 List of allowed values for the field.
106119 requires: list, optional
@@ -118,14 +131,8 @@ class Arg(Field):
118131 If True the input field can’t be provided by the user but it aggregates other
119132 input fields (for example the fields with argstr: -o {fldA} {fldB}), by default
120133 it is False
121- type: type, optional
122- The type of the field, by default it is Any
123- name: str, optional
124- The name of the field, used when specifying a list of fields instead of a mapping
125- from name to field, by default it is None
126134 """
127135
128- default : ty .Any = EMPTY
129136 allowed_values : list | None = None
130137 xor : list | None = None
131138 copy_mode : File .CopyMode = File .CopyMode .any
@@ -145,6 +152,8 @@ class Out(Field):
145152 from name to field, by default it is None
146153 type: type, optional
147154 The type of the field, by default it is Any
155+ default : Any, optional
156+ the default value for the field, by default it is EMPTY
148157 help_string: str, optional
149158 A short description of the input field.
150159 requires: list, optional
@@ -385,7 +394,7 @@ def make_outputs_spec(
385394 f"Cannot make { spec_type } output spec from { out_spec_bases } bases"
386395 )
387396 outputs_bases = bases + (spec_type ,)
388- if reserved_names := [n for n in outputs if n in RESERVED_OUTPUT_NAMES ]:
397+ if reserved_names := [n for n in outputs if n in spec_type . RESERVED_FIELD_NAMES ]:
389398 raise ValueError (
390399 f"{ reserved_names } are reserved and cannot be used for output field names"
391400 )
0 commit comments