Skip to content

Commit

Permalink
Merge pull request #41 from qua-platform/fix/dict-instantiation
Browse files Browse the repository at this point in the history
fix dict instantiation
  • Loading branch information
nulinspiratie authored Jul 3, 2024
2 parents 5e55155 + 2aa9de6 commit f187fb3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
29 changes: 8 additions & 21 deletions quam/core/quam_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,14 @@ def instantiate_attrs_from_dict(

instantiated_attr_dict = {}
for attr_name, attr_val in attr_dict.items():
if not required_subtype:
instantiated_attr_dict[attr_name] = attr_val
continue
if string_reference.is_reference(attr_val):
instantiated_attr_dict[attr_name] = attr_val
continue
if isclass(required_subtype) and issubclass(required_subtype, QuamComponent):
instantiated_attr = instantiate_quam_class(
required_subtype,
attr_val,
fix_attrs=fix_attrs,
validate_type=validate_type,
str_repr=f'{str_repr}["{attr_name}"]',
)
else:
instantiated_attr = attr_val
# Add custom __class__ QuamComponent logic here

validate_obj_type(instantiated_attr, required_subtype, str_repr=str_repr)

instantiated_attr_dict[attr_name] = instantiated_attr
instantiated_attr_dict[attr_name] = instantiate_attr(
attr_val=attr_val,
expected_type=required_subtype,
allow_none=False,
fix_attrs=fix_attrs,
validate_type=validate_type if required_subtype is not None else False,
str_repr=f'{str_repr}["{attr_name}"]',
)

return instantiated_attr_dict

Expand Down
21 changes: 21 additions & 0 deletions tests/instantiation/test_instantiate_explicit_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,24 @@ def test_instantiate_explicit_class_with_union_type():
assert isinstance(component, QuamOuterComponent)
assert isinstance(component.list_basic_components[0], QuamBasicComponent)
assert component.to_dict() == quam_component.to_dict()


@quam_dataclass
class Component1(QuamComponent):
pass

@quam_dataclass
class Component2(QuamComponent):
pass


def test_instantiation_unknown_error():


instantiate_attr(
attr_val={"a": {"__class__": get_full_class_path(Component1)}},
expected_type=Dict[str, Union[Component1, Component2]],
fix_attrs=True,
validate_type=True,
str_repr="hi"
)

0 comments on commit f187fb3

Please sign in to comment.