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

Support int keys for QuamDict #18

Merged
merged 1 commit into from
Mar 8, 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
2 changes: 1 addition & 1 deletion quam/core/quam_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def convert_dict_and_list(value, cls_or_obj=None, attr=None):
"""Convert a dict or list to a QuamDict or QuamList if possible."""
if isinstance(value, dict):
value_annotation = _get_value_annotation(cls_or_obj=cls_or_obj, attr=attr)
return QuamDict(**value, value_annotation=value_annotation)
return QuamDict(value, value_annotation=value_annotation)
elif type(value) == list:
value_annotation = _get_value_annotation(cls_or_obj=cls_or_obj, attr=attr)
return QuamList(value, value_annotation=value_annotation)
Expand Down
8 changes: 5 additions & 3 deletions quam/utils/string_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ def get_relative_reference_value(obj, string: str) -> Any:
except KeyError as e:
raise AttributeError(f"Object {obj} has no attribute {next_attr}") from e
elif isinstance(obj, (dict, UserDict)):
try:
if next_attr in obj:
obj_attr = obj[next_attr]
except KeyError as e:
raise AttributeError(f"Object {obj} has no attribute {next_attr}") from e
elif next_attr.isdigit() and int(next_attr) in obj:
obj_attr = obj[int(next_attr)]
else:
raise AttributeError(f"Object {obj} has no attribute {next_attr}")
else:
obj_attr = getattr(obj, next_attr)

Expand Down
14 changes: 14 additions & 0 deletions tests/quam_base/referencing/test_referencing_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ def test_referencing_to_dict():
assert quam_root._get_referenced_value("#/quam_dict/a") == 44
assert quam_root.quam_dict._get_referenced_value("#/quam_dict/a") == 44
assert quam_root.quam_dict._get_referenced_value("#./a") == 44


def test_referencing_dict_int_keys():
quam_root = BareQuamRoot()
quam_root.quam_dict = {"1": 1, 2: 2}

assert list(quam_root.quam_dict.keys()) == list(quam_root.quam_dict)
assert list(quam_root.quam_dict) == ["1", 2]

assert quam_root._get_referenced_value("#/quam_dict/1") == 1
assert quam_root.quam_dict._get_referenced_value("#/quam_dict/1") == 1

assert quam_root._get_referenced_value("#/quam_dict/2") == 2
assert quam_root.quam_dict._get_referenced_value("#/quam_dict/2") == 2
10 changes: 10 additions & 0 deletions tests/quam_base/test_quam_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,13 @@ def test_dict_unreferenced_value():
assert d.val1 == 42
assert d.val2 == 42
assert d.get_unreferenced_value("val1") == "#./val2"


def test_quam_dict_int_keys():
quam_dict = QuamDict({1: 2})
assert quam_dict.data == {1: 2}
assert quam_dict[1] == 2
quam_dict.pop(1)
assert quam_dict.data == {}
with pytest.raises(KeyError):
quam_dict[1]