Skip to content

Commit

Permalink
Add try except for union fields
Browse files Browse the repository at this point in the history
  • Loading branch information
yonatanrqm committed Jun 16, 2024
1 parent c29b461 commit 45b796a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions quam/core/quam_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ def instantiate_attr(
)
if typing.get_origin(expected_type) == dict:
expected_type = dict
elif typing.get_origin(expected_type) == typing.Union:
for union_type in typing.get_args(expected_type):
try:
instantiated_attr = instantiate_attr(
attr_val=attr_val,
expected_type=union_type,
allow_none=allow_none,
fix_attrs=fix_attrs,
validate_type=validate_type,
str_repr=str_repr,
)
break
except TypeError:
continue
elif (
isinstance(expected_type, list)
or typing.get_origin(expected_type) == list
Expand All @@ -240,8 +254,6 @@ def instantiate_attr(
expected_type = list
elif typing.get_origin(expected_type) == tuple:
instantiated_attr = tuple(instantiated_attr)
elif typing.get_origin(expected_type) == typing.Union:
instantiated_attr = attr_val
elif typing.get_origin(expected_type) == tuple:
if isinstance(attr_val, list):
attr_val = tuple(attr_val)
Expand Down

0 comments on commit 45b796a

Please sign in to comment.