Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dict instantiation #41

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions quam/core/quam_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,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"
)