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

Instrument convert attrs to properties #4417

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
83 changes: 61 additions & 22 deletions src/qcodes/instrument/instrument_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,13 @@
self.label = name if label is None else label
self._label: str

self.parameters: dict[str, ParameterBase] = {}
"""
All the parameters supported by this instrument.
Usually populated via :py:meth:`add_parameter`.
"""
self.functions: dict[str, Function] = {}
"""
All the functions supported by this
instrument. Usually populated via :py:meth:`add_function`.
"""
self.submodules: dict[str, InstrumentModule | ChannelTuple] = {}
"""
All the submodules of this instrument
such as channel lists or logical groupings of parameters.
Usually populated via :py:meth:`add_submodule`.
"""
self.instrument_modules: dict[str, InstrumentModule] = {}
"""
All the :class:`InstrumentModule` of this instrument
Usually populated via :py:meth:`add_submodule`.
"""
self._parameters: dict[str, ParameterBase] = {}

self._functions: dict[str, Function] = {}

self._submodules: dict[str, InstrumentModule | ChannelTuple] = {}

self._instrument_modules: dict[str, InstrumentModule] = {}

self._channel_lists: dict[str, ChannelTuple] = {}
"""
Expand All @@ -124,6 +110,55 @@
def label(self, label: str) -> None:
self._label = label

@property
def parameters(self) -> dict[str, ParameterBase]:
"""
All the parameters supported by this instrument.
Usually populated via :py:meth:`add_parameter`.
"""
return self._parameters

@parameters.setter
def parameters(self, value: dict[str, ParameterBase]) -> None:
self._parameters = value

Check warning on line 123 in src/qcodes/instrument/instrument_base.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/instrument/instrument_base.py#L123

Added line #L123 was not covered by tests

@property
def functions(self) -> dict[str, Function]:
"""
All the functions supported by this
instrument. Usually populated via :py:meth:`add_function`.
"""
return self._functions

@functions.setter
def functions(self, value: dict[str, Function]) -> None:
self._functions = value

Check warning on line 135 in src/qcodes/instrument/instrument_base.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/instrument/instrument_base.py#L135

Added line #L135 was not covered by tests

@property
def submodules(self) -> dict[str, InstrumentModule | ChannelTuple]:
"""
All the submodules of this instrument
such as channel lists or logical groupings of parameters.
Usually populated via :py:meth:`add_submodule`.
"""
return self._submodules

@submodules.setter
def submodules(self, value: dict[str, InstrumentModule | ChannelTuple]) -> None:
self._submodules = value

Check warning on line 148 in src/qcodes/instrument/instrument_base.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/instrument/instrument_base.py#L148

Added line #L148 was not covered by tests

@property
def instrument_modules(self) -> dict[str, InstrumentModule]:
"""
All the :class:`InstrumentModule` of this instrument
Usually populated via :py:meth:`add_submodule`.
"""
return self._instrument_modules

@instrument_modules.setter
def instrument_modules(self, value: dict[str, InstrumentModule]) -> None:
self._instrument_modules = value

Check warning on line 160 in src/qcodes/instrument/instrument_base.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/instrument/instrument_base.py#L160

Added line #L160 was not covered by tests

def add_parameter(
self,
name: str,
Expand Down Expand Up @@ -666,7 +701,11 @@
# instrument.get('someparam') === instrument['someparam'].get() #
# etc... #
#
delegate_attr_dicts: ClassVar[list[str]] = ["parameters", "functions", "submodules"]
delegate_attr_dicts: ClassVar[list[str]] = [
"_parameters",
"_functions",
"_submodules",
]

@deprecated(
"Use attributes directly on the instrument object instead.",
Expand Down
12 changes: 10 additions & 2 deletions src/qcodes/metadatable/metadatable_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@


class Metadatable:
def __init__(self, metadata: Optional["Mapping[str, Any]"] = None):
self.metadata: dict[str, Any] = {}
def __init__(self, metadata: "Mapping[str, Any] | None" = None):
self._metadata: dict[str, Any] = {}
self.load_metadata(metadata or {})

@property
def metadata(self) -> dict[str, Any]:
return self._metadata

@metadata.setter
def metadata(self, metadata: dict[str, Any]) -> None:
self._metadata = metadata

Check warning on line 32 in src/qcodes/metadatable/metadatable_base.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/metadatable/metadatable_base.py#L32

Added line #L32 was not covered by tests

def load_metadata(self, metadata: "Mapping[str, Any]") -> None:
"""
Load metadata into this classes metadata dictionary.
Expand Down
29 changes: 16 additions & 13 deletions tests/sphinx_extension/test_parse_parameter_attr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from sphinx.util.inspect import safe_getattr

from qcodes.instrument import InstrumentBase, VisaInstrument
from qcodes.instrument import InstrumentBase
from qcodes.parameters import Parameter
from qcodes.sphinx_extensions.parse_parameter_attr import (
ParameterProxy,
qcodes_parameter_attr_getter,
Expand All @@ -23,8 +24,13 @@ def __init__(self, *args, **kwargs):
An instance attribute
"""

self.my_param = Parameter(
name="my_param", instrument=self, get_cmd=None, set_cmd=None
)
"""A QCoDeS Parameter"""

class DummyNoInitClass(InstrumentBase):

class DummyNoInitClass(DummyTestClass):
myattr: str = "ClassAttribute"
"""
A class attribute
Expand Down Expand Up @@ -83,16 +89,13 @@ def test_extract_instance_attr() -> None:
assert repr(b) == '"InstanceAttribute"'


def test_instrument_base_get_attr() -> None:
parameters = qcodes_parameter_attr_getter(InstrumentBase, "parameters")
def test_parameter_get_attr():
parameters = qcodes_parameter_attr_getter(DummyTestClass, "my_param")
assert isinstance(parameters, ParameterProxy)
assert repr(parameters) == "{}"


def test_visa_instr_get_attr() -> None:
parameters = qcodes_parameter_attr_getter(VisaInstrument, "parameters")
assert isinstance(parameters, ParameterProxy)
assert repr(parameters) == "{}"
assert (
repr(parameters)
== 'Parameter( name="my_param", instrument=self, get_cmd=None, set_cmd=None )'
)


def test_decorated_init_func() -> None:
Expand All @@ -109,6 +112,6 @@ def test_decorated_class() -> None:

def test_no_init() -> None:
"""Test that attribute can be found from a class without an init function."""
attr = qcodes_parameter_attr_getter(DummyNoInitClass, "parameters")
attr = qcodes_parameter_attr_getter(DummyNoInitClass, "other_attr")
assert isinstance(attr, ParameterProxy)
assert repr(attr) == "{}"
assert repr(attr) == '"InstanceAttribute"'
Loading