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

Feat/improved instantiation #23

Merged
merged 3 commits into from
Mar 22, 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
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [Unreleased]
### Fixed
- Switched channel `RF_inputs` and `RF_outputs` for Octave
- Loading QuAM components when the expected type is a union or the actual type is a list
no longer raises an error


## [0.3.0]
Expand Down
3 changes: 2 additions & 1 deletion quam/core/quam_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ def to_dict(
follow_references=follow_references,
include_defaults=include_defaults,
)
if not self._val_matches_attr_annotation(attr, val):
val_is_list = isinstance(val, (list, UserList))
if not self._val_matches_attr_annotation(attr, val) and not val_is_list:
quam_dict[attr]["__class__"] = get_full_class_path(val)
else:
quam_dict[attr] = val
Expand Down
37 changes: 24 additions & 13 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 @@ -205,7 +221,11 @@ def instantiate_attr(
)
if typing.get_origin(expected_type) == dict:
expected_type = dict
elif isinstance(expected_type, list) or typing.get_origin(expected_type) == list:
elif (
isinstance(expected_type, list)
or typing.get_origin(expected_type) == list
or isinstance(attr_val, list)
):
instantiated_attr = instantiate_attrs_from_list(
attr_list=attr_val,
required_type=expected_type,
Expand All @@ -216,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
35 changes: 35 additions & 0 deletions tests/instantiation/test_instantiate_explicit_class.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from typing import List, Union
from quam.core import *
from quam.core.quam_instantiation import *
from quam.utils import get_full_class_path
Expand All @@ -25,3 +26,37 @@ def test_instantiate_from_class():
assert type(loaded_explicit_component) == type(loaded_explicit_component2)

assert isinstance(loaded_explicit_component, QuamComponentTest)


def test_instantiate_nondefault_list_from_dict():
@quam_dataclass
class QuamBasicComponent(QuamComponent):
l: Union[int, List[int]] = 42

quam_component = QuamBasicComponent(l=[1, 2, 3])

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()
5 changes: 5 additions & 0 deletions tests/quam_base/test_quam_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ def test_list_get_attrs_error():
quam_list = QuamList()
with pytest.raises(NotImplementedError):
quam_list.get_attrs()


def test_list_to_dict():
quam_list = QuamList([1, 2, 3])
assert quam_list.to_dict() == [1, 2, 3]
9 changes: 9 additions & 0 deletions tests/quam_base/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ class QuamBasicComponent(QuamComponent):

quam_component = QuamBasicComponent(l=[1, 2, 3])
assert quam_component.to_dict() == {"l": [1, 2, 3]}


def test_list_to_dict_nondefault():
@quam_dataclass
class QuamBasicComponent(QuamComponent):
l: int = 42

quam_component = QuamBasicComponent(l=[1, 2, 3])
assert quam_component.to_dict() == {"l": [1, 2, 3]}