diff --git a/CHANGELOG.md b/CHANGELOG.md index ae90013d..003b1734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/quam/core/quam_instantiation.py b/quam/core/quam_instantiation.py index d6674843..0cf0fe8c 100644 --- a/quam/core/quam_instantiation.py +++ b/quam/core/quam_instantiation.py @@ -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 diff --git a/tests/instantiation/test_instantiation.py b/tests/instantiation/test_instantiation.py index f6c0da63..d6072722 100644 --- a/tests/instantiation/test_instantiation.py +++ b/tests/instantiation/test_instantiation.py @@ -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] @@ -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"]]}, + )