Skip to content

Commit 44fedd4

Browse files
committed
Improve InstrumentRef parameter
Support looking up any Insturment as long as the name is know not just the same as the bound instrument
1 parent 0bb16b1 commit 44fedd4

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

src/qcodes/parameters/parameter_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from types import TracebackType
4343

4444
from qcodes.dataset.data_set_protocol import ValuesType
45-
from qcodes.instrument import InstrumentBase
45+
from qcodes.instrument import Instrument, InstrumentBase
4646
from qcodes.logger.instrument_logger import InstrumentLoggerAdapter
4747
ParameterDataTypeVar = TypeVar("ParameterDataTypeVar", default=Any)
4848
# InstrumentTypeVar_co is a covariant type variable representing the instrument
@@ -1073,7 +1073,7 @@ def instrument(self) -> InstrumentTypeVar_co:
10731073
return self._instrument
10741074

10751075
@property
1076-
def root_instrument(self) -> InstrumentBase | None:
1076+
def root_instrument(self) -> Instrument | None:
10771077
"""
10781078
Return the fundamental instrument that this parameter belongs too.
10791079
E.g if the parameter is bound to a channel this will return the

src/qcodes/parameters/specialized_parameters.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
from __future__ import annotations
88

99
from time import perf_counter
10-
from typing import TYPE_CHECKING, Any, Literal
10+
from typing import TYPE_CHECKING, Any, Generic, Literal
1111

12+
from qcodes.parameters.parameter_base import InstrumentTypeVar_co
1213
from qcodes.validators import Strings, Validator
1314

1415
from .parameter import Parameter
1516

1617
if TYPE_CHECKING:
1718
from collections.abc import Callable
1819

19-
from qcodes.instrument import InstrumentBase
20+
from qcodes.instrument import Instrument
2021

2122

2223
class ElapsedTimeParameter(Parameter):
@@ -53,7 +54,9 @@ def t0(self) -> float:
5354
return self._t0
5455

5556

56-
class InstrumentRefParameter(Parameter):
57+
class InstrumentRefParameter(
58+
Parameter[str, InstrumentTypeVar_co], Generic[InstrumentTypeVar_co]
59+
):
5760
"""
5861
An instrument reference parameter.
5962
@@ -78,12 +81,12 @@ class InstrumentRefParameter(Parameter):
7881
def __init__(
7982
self,
8083
name: str,
81-
instrument: InstrumentBase | None = None,
84+
instrument: InstrumentTypeVar_co = None,
8285
label: str | None = None,
8386
unit: str | None = None,
8487
get_cmd: str | Callable[..., Any] | Literal[False] | None = None,
8588
set_cmd: str | Callable[..., Any] | Literal[False] | None = None,
86-
initial_value: float | str | None = None,
89+
initial_value: str | None = None,
8790
max_val_age: float | None = None,
8891
vals: Validator[Any] | None = None,
8992
docstring: str | None = None,
@@ -107,16 +110,15 @@ def __init__(
107110
**kwargs,
108111
)
109112

110-
# TODO(nulinspiratie) check class works now it's subclassed from Parameter
111-
def get_instr(self) -> InstrumentBase:
113+
def get_instr(self) -> Instrument | None:
112114
"""
113115
Returns the instance of the instrument with the name equal to the
114116
value of this parameter.
115117
"""
118+
# lazy import to avoid circular import
119+
# since Instrument module depends on parameer module
120+
from qcodes.instrument import Instrument # noqa: PLC0415
121+
116122
ref_instrument_name = self.get()
117-
# note that _instrument refers to the instrument this parameter belongs
118-
# to, while the ref_instrument_name is the instrument that is the value
119-
# of this parameter.
120-
if self._instrument is None:
121-
raise RuntimeError("InstrumentRefParameter is not bound to an instrument.")
122-
return self._instrument.find_instrument(ref_instrument_name)
123+
124+
return Instrument.find_instrument(ref_instrument_name)

tests/parameter/test_instrument_ref_parameter.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,32 @@
99
from collections.abc import Generator
1010

1111

12+
class DummyHolder(DummyInstrument):
13+
def __init__(self, name: str) -> None:
14+
super().__init__(name)
15+
self.test = self.add_parameter(
16+
"test",
17+
parameter_class=InstrumentRefParameter,
18+
initial_value=None,
19+
)
20+
21+
1222
@pytest.fixture(name="instrument_a")
13-
def _make_instrument_a() -> "Generator[DummyInstrument, None, None]":
14-
a = DummyInstrument("dummy_holder")
15-
try:
16-
yield a
17-
finally:
18-
a.close()
23+
def _make_instrument_a() -> "Generator[DummyHolder, None, None]":
24+
25+
a = DummyHolder("dummy_holder")
26+
yield a
27+
a.close()
1928

2029

2130
@pytest.fixture(name="instrument_d")
2231
def _make_instrument_d() -> "Generator[DummyInstrument, None, None]":
2332
d = DummyInstrument("dummy")
24-
try:
25-
yield d
26-
finally:
27-
d.close()
33+
yield d
34+
d.close()
2835

2936

30-
def test_get_instr(
31-
instrument_a: DummyInstrument, instrument_d: DummyInstrument
32-
) -> None:
33-
instrument_a.add_parameter("test", parameter_class=InstrumentRefParameter)
37+
def test_get_instr(instrument_a: DummyHolder, instrument_d: DummyInstrument) -> None:
3438

3539
instrument_a.test.set(instrument_d.name)
3640

0 commit comments

Comments
 (0)