Skip to content

Commit

Permalink
Merge pull request #60 from qua-platform/fix/tuple-weights-instantiation
Browse files Browse the repository at this point in the history
Fix: Allow list tuples within Union to be instantiated properly
  • Loading branch information
nulinspiratie authored Jul 14, 2024
2 parents 2c79eaa + b0365c0 commit c8bd4a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Fix quam object instantiation error when a parameter type uses pipe operator
- Allow int keys to be serialised / loaded in QuAM using JSONSerialiser
- Fix type `OctaveUpconverter.triggered_reersed` -> `OctaveUpconverter.triggered_reversed`
- Fix tuples not being instantiated properly in specific circumstances


## [0.3.3]
Expand Down
46 changes: 7 additions & 39 deletions quam/core/quam_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,48 +105,16 @@ def instantiate_attrs_from_list(

instantiated_attr_list = []
for k, attr_val in enumerate(attr_list):
if isinstance(attr_val, dict) and "__class__" in attr_val:
instantiated_attr = instantiate_quam_class(
get_class_from_path(attr_val["__class__"]),
attr_val,
instantiated_attr_list.append(
instantiate_attr(
attr_val=attr_val,
expected_type=required_subtype,
allow_none=False,
fix_attrs=fix_attrs,
validate_type=validate_type,
validate_type=validate_type if required_subtype is not None else False,
str_repr=f"{str_repr}[{k}]",
)
elif not required_subtype:
instantiated_attr_list.append(attr_val)
continue
elif typing.get_origin(required_subtype) == list:
assert typing.get_origin(required_subtype) == list

instantiated_attr = instantiate_attrs_from_list(
attr_list=attr_val,
required_type=required_subtype,
fix_attrs=fix_attrs,
validate_type=validate_type,
str_repr=f"{str_repr}[{k}]",
)
elif not isclass(required_subtype):
instantiated_attr = attr_val
elif issubclass(required_subtype, QuamComponent):
if string_reference.is_reference(attr_val):
instantiated_attr = attr_val
else:
instantiated_attr = instantiate_quam_class(
required_subtype,
attr_val,
fix_attrs=fix_attrs,
validate_type=validate_type,
str_repr=f"{str_repr}[{k}]",
)
else:
instantiated_attr = attr_val
# Add custom __class__ QuamComponent logic here
if required_subtype:
validate_obj_type(instantiated_attr, required_subtype, str_repr=str_repr)

instantiated_attr_list.append(instantiated_attr)

)
return instantiated_attr_list


Expand Down
13 changes: 12 additions & 1 deletion tests/instantiation/test_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def test_instance_attr_literal_fail():
)


def test_isntantiate_tuple():
def test_instantiate_tuple():
@quam_dataclass
class TestQuamTuple(QuamComponent):
tuple_val: Tuple[int, str]
Expand Down Expand Up @@ -376,3 +376,14 @@ class TestQuamUnion(QuamComponent):

with pytest.raises(TypeError):
instantiate_quam_class(TestQuamUnion, {"union_val": {"a": "42"}})


def test_instantiation_nested_tuple():
@quam_dataclass
class NestedTupleComponent(QuamComponent):
nested_tuple: Union[List[Tuple[int, str]], List[Tuple[int]]]

instantiate_quam_class(
quam_class=NestedTupleComponent,
contents={"nested_tuple": [[1, "a"], [2, "b"]]},
)

0 comments on commit c8bd4a9

Please sign in to comment.