Skip to content

Commit 27a0404

Browse files
committed
fixed support for multi-input objs
1 parent 8bfbe8c commit 27a0404

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pydra/utils/typing.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ def expand_and_coerce(obj, pattern: ty.Union[type, tuple]):
279279
if not isinstance(pattern, tuple):
280280
return coerce_basic(obj, pattern)
281281
origin, pattern_args = pattern
282+
if origin == MultiInputObj:
283+
return coerce_multi_input(obj, pattern_args)
282284
if origin in UNION_TYPES:
283285
return coerce_union(obj, pattern_args)
284286
if origin is type:
@@ -331,6 +333,21 @@ def coerce_union(obj, pattern_args):
331333
+ "\n\n".join(f"{a} -> {e}" for a, e in zip(pattern_args, reasons))
332334
)
333335

336+
def coerce_multi_input(obj, pattern_args):
337+
# Attempt to coerce the object into arg type of the MultiInputObj first,
338+
# and if that fails, try to coerce it into a list of the arg type
339+
try:
340+
return coerce_sequence(list, obj, pattern_args)
341+
except TypeError as e1:
342+
try:
343+
return [expand_and_coerce(obj, pattern_args)]
344+
except TypeError as e2:
345+
raise TypeError(
346+
f"Could not coerce object ({obj!r}) to MultiInputObj[{pattern_args[0]}] "
347+
f"either as sequence of {pattern_args[0]} ({e1}) or a single {pattern_args[0]} "
348+
f"object to be wrapped in a list {e2}"
349+
) from e2
350+
334351
def coerce_mapping(
335352
obj: ty.Mapping, type_: ty.Type[ty.Mapping], pattern_args: list
336353
):
@@ -407,26 +424,7 @@ def coerce_obj(obj, type_):
407424
f"Cannot coerce {obj!r} into {type_}{msg}{self.label_str}"
408425
) from e
409426

410-
try:
411-
return expand_and_coerce(object_, self.pattern)
412-
except TypeError as e:
413-
# Defial handling for MultiInputObjects (which are annoying)
414-
if isinstance(self.pattern, tuple) and self.pattern[0] == MultiInputObj:
415-
# Attempt to coerce the object into arg type of the MultiInputObj first,
416-
# and if that fails, try to coerce it into a list of the arg type
417-
inner_type_parser = copy(self)
418-
inner_type_parser.pattern = self.pattern[1][0]
419-
try:
420-
return [inner_type_parser.coerce(object_)]
421-
except TypeError:
422-
add_exc_note(
423-
e,
424-
"Also failed to coerce to the arg-type of the MultiInputObj "
425-
f"({self.pattern[1][0]})",
426-
)
427-
raise e
428-
else:
429-
raise e
427+
return expand_and_coerce(object_, self.pattern)
430428

431429
def check_type(self, type_: ty.Type[ty.Any]):
432430
"""Checks the given type to see whether it matches or is a subtype of the
@@ -589,7 +587,7 @@ def check_sequence(tp_args, pattern_args):
589587
try:
590588
return expand_and_check(type_, self.pattern)
591589
except TypeError as e:
592-
# Defial handling for MultiInputObjects (which are annoying)
590+
# Special handling for MultiInputObjects (which are annoying)
593591
if not isinstance(self.pattern, tuple) or self.pattern[0] != MultiInputObj:
594592
raise e
595593
# Attempt to coerce the object into arg type of the MultiInputObj first,

0 commit comments

Comments
 (0)