diff --git a/src/qcodes/instrument/instrument_base.py b/src/qcodes/instrument/instrument_base.py index 3323f950e8e..531a2554eaf 100644 --- a/src/qcodes/instrument/instrument_base.py +++ b/src/qcodes/instrument/instrument_base.py @@ -77,27 +77,13 @@ def __init__( 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] = {} """ @@ -124,6 +110,55 @@ def label(self) -> str: 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 + + @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 + + @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 + + @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 + def add_parameter( self, name: str, @@ -666,7 +701,11 @@ def _is_abstract(self) -> bool: # 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.", diff --git a/src/qcodes/metadatable/metadatable_base.py b/src/qcodes/metadatable/metadatable_base.py index 8ed766e2a26..856b034500c 100644 --- a/src/qcodes/metadatable/metadatable_base.py +++ b/src/qcodes/metadatable/metadatable_base.py @@ -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 + def load_metadata(self, metadata: "Mapping[str, Any]") -> None: """ Load metadata into this classes metadata dictionary. diff --git a/tests/sphinx_extension/test_parse_parameter_attr.py b/tests/sphinx_extension/test_parse_parameter_attr.py index 5d81d0032e7..2c2db6056a2 100644 --- a/tests/sphinx_extension/test_parse_parameter_attr.py +++ b/tests/sphinx_extension/test_parse_parameter_attr.py @@ -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, @@ -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 @@ -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: @@ -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"'