Skip to content

Commit 9488fc3

Browse files
committed
Improve InstrumentRef parameter
Support looking up any Instrument as long as the name is known, not just the same as the bound instrument
1 parent 269ad6e commit 9488fc3

2 files changed

Lines changed: 33 additions & 27 deletions

File tree

src/qcodes/parameters/specialized_parameters.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import warnings
1010
from time import perf_counter
11-
from typing import TYPE_CHECKING, Any, ClassVar, Literal
11+
from typing import TYPE_CHECKING, Any, ClassVar, Generic, Literal
1212

13+
from qcodes.parameters.parameter_base import InstrumentTypeVar_co
1314
from qcodes.utils import QCoDeSDeprecationWarning
1415
from qcodes.validators import Strings, Validator
1516

@@ -18,7 +19,7 @@
1819
if TYPE_CHECKING:
1920
from collections.abc import Callable
2021

21-
from qcodes.instrument import InstrumentBase
22+
from qcodes.instrument import Instrument
2223

2324

2425
class ElapsedTimeParameter(Parameter):
@@ -96,7 +97,9 @@ def t0(self) -> float:
9697
return self._t0
9798

9899

99-
class InstrumentRefParameter(Parameter):
100+
class InstrumentRefParameter(
101+
Parameter[str, InstrumentTypeVar_co], Generic[InstrumentTypeVar_co]
102+
):
100103
"""
101104
An instrument reference parameter.
102105
@@ -134,12 +137,12 @@ def __init__(
134137
self,
135138
name: str,
136139
*args: Any,
137-
instrument: InstrumentBase | None = None,
140+
instrument: InstrumentTypeVar_co = None,
138141
label: str | None = None,
139142
unit: str | None = None,
140143
get_cmd: str | Callable[..., Any] | Literal[False] | None = None,
141144
set_cmd: str | Callable[..., Any] | Literal[False] | None = None,
142-
initial_value: float | str | None = None,
145+
initial_value: str | None = None,
143146
max_val_age: float | None = None,
144147
vals: Validator[Any] | None = None,
145148
docstring: str | None = None,
@@ -229,16 +232,15 @@ def __init__(
229232
**kwargs,
230233
)
231234

232-
# TODO(nulinspiratie) check class works now it's subclassed from Parameter
233-
def get_instr(self) -> InstrumentBase:
235+
def get_instr(self) -> Instrument | None:
234236
"""
235237
Returns the instance of the instrument with the name equal to the
236238
value of this parameter.
237239
"""
240+
# lazy import to avoid circular import
241+
# since Instrument module depends on parameer module
242+
from qcodes.instrument import Instrument # noqa: PLC0415
243+
238244
ref_instrument_name = self.get()
239-
# note that _instrument refers to the instrument this parameter belongs
240-
# to, while the ref_instrument_name is the instrument that is the value
241-
# of this parameter.
242-
if self._instrument is None:
243-
raise RuntimeError("InstrumentRefParameter is not bound to an instrument.")
244-
return self._instrument.find_instrument(ref_instrument_name)
245+
246+
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)