Skip to content

Commit

Permalink
fix bug with adding __class__ to list
Browse files Browse the repository at this point in the history
  • Loading branch information
nulinspiratie committed Mar 21, 2024
1 parent 3e9320f commit e90967e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
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
6 changes: 5 additions & 1 deletion quam/core/quam_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,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 Down
12 changes: 12 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,14 @@ 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)
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]}

0 comments on commit e90967e

Please sign in to comment.