Skip to content

Commit

Permalink
fix instantiation in rare cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nulinspiratie committed Mar 21, 2024
1 parent e90967e commit 62a9f21
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ additional
.idea
build
site
calibration_db.json

dependency_links.txt
PKG-INFO
Expand Down
31 changes: 19 additions & 12 deletions quam/core/quam_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ def instantiate_attrs_from_list(

instantiated_attr_list = []
for k, attr_val in enumerate(attr_list):
if not required_subtype:
if isinstance(attr_val, dict) and "__class__" in attr_val:
instantiated_attr = instantiate_quam_class(
get_class_from_path(attr_val["__class__"]),
attr_val,
fix_attrs=fix_attrs,
validate_type=validate_type,
str_repr=f"{str_repr}[{k}]",
)
elif not required_subtype:
instantiated_attr_list.append(attr_val)
continue
elif typing.get_origin(required_subtype) == list:
Expand Down Expand Up @@ -136,8 +144,8 @@ def instantiate_attrs_from_list(
else:
instantiated_attr = attr_val
# Add custom __class__ QuamComponent logic here

validate_obj_type(instantiated_attr, required_subtype, str_repr=str_repr)
if required_subtype:
validate_obj_type(instantiated_attr, required_subtype, str_repr=str_repr)

instantiated_attr_list.append(instantiated_attr)

Expand Down Expand Up @@ -195,6 +203,14 @@ def instantiate_attr(
validate_type=validate_type,
str_repr=str_repr,
)
elif isinstance(attr_val, dict) and "__class__" in attr_val:
instantiated_attr = instantiate_quam_class(
quam_class=expected_type,
contents=attr_val,
fix_attrs=fix_attrs,
validate_type=validate_type,
str_repr=str_repr,
)
elif isinstance(expected_type, dict) or typing.get_origin(expected_type) == dict:
instantiated_attr = instantiate_attrs_from_dict(
attr_dict=attr_val,
Expand All @@ -220,16 +236,7 @@ def instantiate_attr(
if typing.get_origin(expected_type) == list:
expected_type = list
elif typing.get_origin(expected_type) == typing.Union:
if not all(
t in [str, int, float, bool, type(None)]
for t in typing.get_args(expected_type)
):
raise TypeError(
"Currently only Union[str, int, float, bool] is supported, whereas "
f"{expected_type} was found in {str_repr}"
)
instantiated_attr = attr_val

elif typing.get_origin(expected_type) == tuple:
if isinstance(attr_val, list):
attr_val = tuple(attr_val)
Expand Down
23 changes: 23 additions & 0 deletions tests/instantiation/test_instantiate_explicit_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ class QuamBasicComponent(QuamComponent):

d = quam_component.to_dict()
instantiate_quam_class(QuamBasicComponent, d)


@quam_dataclass
class QuamBasicComponent(QuamComponent):
int_val: int = 42


@quam_dataclass
class QuamOuterComponent(QuamComponent):
list_basic_components: Union[int, List[QuamBasicComponent]]


def test_instantiate_explicit_class_with_union_type():
quam_component = QuamOuterComponent(
list_basic_components=[QuamBasicComponent(int_val=42)]
)

d = quam_component.to_dict()
component = instantiate_quam_class(QuamOuterComponent, d)

assert isinstance(component, QuamOuterComponent)
assert isinstance(component.list_basic_components[0], QuamBasicComponent)
assert component.to_dict() == quam_component.to_dict()

0 comments on commit 62a9f21

Please sign in to comment.