Skip to content

Commit 756825b

Browse files
committed
generalize is_fileset_or_union utility to is_subclass_or_union
1 parent fc3a6ce commit 756825b

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

pydra/utils/typing.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,16 +1122,42 @@ def is_fileset_or_union(type_: type, allow_none: bool | None = None) -> bool:
11221122
is_fileset : bool
11231123
whether the type is a FileSet or a Union containing a FileSet
11241124
"""
1125+
return is_subclass_or_union(type_, core.FileSet, allow_none=allow_none)
1126+
1127+
1128+
def is_subclass_or_union(
1129+
type_: type, reference: type, allow_none: bool | None = None
1130+
) -> bool:
1131+
"""Check if the type is a subclass of given reference or a Union containing
1132+
that reference type
1133+
1134+
Parameters
1135+
----------
1136+
type_ : type
1137+
the type to check
1138+
reference : type
1139+
the reference type to check whether the type is a sub-class of or not
1140+
allow_none : bool, optional
1141+
whether to allow None as a valid type, by default None. If None, then None
1142+
is not allowed at the outer layer, but is allowed within a Union
1143+
1144+
Returns
1145+
-------
1146+
bool
1147+
whether the type is a FileSet or a Union containing a FileSet
1148+
"""
11251149
if type_ is None and allow_none:
11261150
return True
11271151
if is_union(type_):
11281152
return any(
1129-
is_fileset_or_union(t, allow_none=allow_none or allow_none is None)
1153+
is_subclass_or_union(
1154+
t, reference, allow_none=allow_none or allow_none is None
1155+
)
11301156
for t in ty.get_args(type_)
11311157
)
11321158
elif not inspect.isclass(type_):
11331159
return False
1134-
return issubclass(type_, core.FileSet)
1160+
return issubclass(type_, reference)
11351161

11361162

11371163
def is_type(*args: ty.Any) -> bool:

0 commit comments

Comments
 (0)