Skip to content

Commit

Permalink
Improved verbosity of the CommandTable header and table invalid attri…
Browse files Browse the repository at this point in the history
…bute exception
  • Loading branch information
markheik committed Jan 27, 2023
1 parent 0a28506 commit 9ef3ce8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# zhinst-toolkit Changelog

## Version 0.5.2

Unreleased

* Improved verbosity of the error message when invalid attributes of `CommandTable.header` and `CommandTable.table` are used.

## Version 0.5.1
* Added full support for the following LabOne modules (no need to fallback to zhinst.core):
* Impedance Module
Expand Down
14 changes: 12 additions & 2 deletions src/zhinst/toolkit/command_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def __getattr__(self, name: str) -> t.Union["ParentEntry", t.Any]:
return self._childs[name]
if name in self._attributes:
return self._properties.get(name, None)
raise AttributeError(name)
raise AttributeError(
f"{name}. Available entries: {self._available_attributes()}"
)

def __setattr__(self, name: str, value: t.Any):
if name.startswith("_"):
Expand All @@ -160,7 +162,15 @@ def __setattr__(self, name: str, value: t.Any):
elif value is None and name in self._childs:
self._childs.pop(name)
else:
raise AttributeError(name)
raise AttributeError(
f"{name}. Available entries: {self._available_attributes()}"
)

def _available_attributes(self) -> t.List[str]:
"""Available property attributes for the instance."""
return list(
self._attributes.keys() if self._attributes else self._child_props.keys()
)

def as_dict(self) -> dict:
"""Return a dictionary presentation of the table node.
Expand Down
17 changes: 14 additions & 3 deletions tests/command_table/test_command_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,20 @@ def test_parent_entry_properties(command_table_schema):
assert hasattr(ct.table[0], property_) is True


def test_property_not_existing(command_table):
with pytest.raises(AttributeError):
getattr(command_table.table[0], "this_property_cannot_exists")
def test_property_get_not_existing(command_table):
properties = (
f'{list(command_table._ct_schema["definitions"]["entry"]["properties"].keys())}'
)
with pytest.raises(AttributeError, match=properties):
command_table.table[0].this_property_cannot_exists


def test_property_set_not_existing(command_table):
properties = (
f'{list(command_table._ct_schema["definitions"]["entry"]["properties"].keys())}'
)
with pytest.raises(AttributeError, match=properties):
command_table.table[0].this_property_cannot_exists = 123


def test_parent_entry_header(command_table_schema):
Expand Down

0 comments on commit 9ef3ce8

Please sign in to comment.