Skip to content

Commit dce5c6f

Browse files
authored
Merge pull request #6086 from jenshnielsen/deprecate_getters
Deprecate convenience methods on Instrument classes
2 parents 36b1817 + f02bdc2 commit dce5c6f

7 files changed

Lines changed: 49 additions & 21 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The methods `get`, `set`, `call` and `__getitem__` on the `InstrumentBase` class have been deprecated.
2+
Parameters can be looked up by name using the `Instrument.parameters` dict and functions using `instrument.functions`
3+
which is cleaner and fully equivalent.

docs/examples/DataSet/Offline Plotting Tutorial.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@
977977
"name": "python",
978978
"nbconvert_exporter": "python",
979979
"pygments_lexer": "ipython3",
980-
"version": "3.12.3"
980+
"version": "3.12.4"
981981
},
982982
"toc": {
983983
"base_numbering": 1,

src/qcodes/instrument/instrument.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def connect_message(
129129
# start with an empty dict, just in case an instrument doesn't
130130
# heed our request to return all 4 fields.
131131
idn = {"vendor": None, "model": None, "serial": None, "firmware": None}
132-
idn.update(self.get(idn_param))
132+
idn.update(self.parameters[idn_param].get())
133133
t = time.time() - (begin_time or self._t0)
134134

135135
con_msg = (

src/qcodes/instrument/instrument_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,21 @@ def _is_abstract(self) -> bool:
660660
#
661661
delegate_attr_dicts: ClassVar[list[str]] = ["parameters", "functions", "submodules"]
662662

663+
@deprecated(
664+
"Use attributes directly on the instrument object instead.",
665+
category=QCoDeSDeprecationWarning,
666+
)
663667
def __getitem__(self, key: str) -> Callable[..., Any] | Parameter:
664668
"""Delegate instrument['name'] to parameter or function 'name'."""
665669
try:
666670
return self.parameters[key]
667671
except KeyError:
668672
return self.functions[key]
669673

674+
@deprecated(
675+
"Call set directly on the parameter.",
676+
category=QCoDeSDeprecationWarning,
677+
)
670678
def set(self, param_name: str, value: Any) -> None:
671679
"""
672680
Shortcut for setting a parameter from its name and new value.
@@ -677,6 +685,10 @@ def set(self, param_name: str, value: Any) -> None:
677685
"""
678686
self.parameters[param_name].set(value)
679687

688+
@deprecated(
689+
"Call get directly on the parameter.",
690+
category=QCoDeSDeprecationWarning,
691+
)
680692
def get(self, param_name: str) -> Any:
681693
"""
682694
Shortcut for getting a parameter from its name.
@@ -689,6 +701,10 @@ def get(self, param_name: str) -> Any:
689701
"""
690702
return self.parameters[param_name].get()
691703

704+
@deprecated(
705+
"Call the function directly.",
706+
category=QCoDeSDeprecationWarning,
707+
)
692708
def call(self, func_name: str, *args: Any) -> Any:
693709
"""
694710
Shortcut for calling a function from its name.

src/qcodes/instrument_drivers/tektronix/AWG5014.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ def __init__(
491491
get_parser=float,
492492
)
493493

494-
self.set("trigger_impedance", 50)
495-
if self.get("clock_freq") != 1e9:
494+
self.trigger_impedance.set(50)
495+
if self.clock_freq.get() != 1e9:
496496
log.info("AWG clock freq not set to 1GHz")
497497

498498
self.connect_message()
@@ -657,12 +657,12 @@ def all_channels_on(self) -> None:
657657
defined waveforms can be ON.
658658
"""
659659
for i in range(1, self.num_channels + 1):
660-
self.set(f"ch{i}_state", 1)
660+
self.parameters[f"ch{i}_state"].set(1)
661661

662662
def all_channels_off(self) -> None:
663663
"""Set the state of all channels to be OFF."""
664664
for i in range(1, self.num_channels + 1):
665-
self.set(f"ch{i}_state", 0)
665+
self.parameters[f"ch{i}_state"].set(0)
666666

667667
#####################
668668
# Sequences section #
@@ -947,7 +947,7 @@ def generate_sequence_cfg(self) -> dict[str, float]:
947947
log.info("Generating sequence_cfg")
948948

949949
AWG_sequence_cfg = {
950-
"SAMPLING_RATE": self.get("clock_freq"),
950+
"SAMPLING_RATE": self.clock_freq.get(),
951951
"CLOCK_SOURCE": (
952952
1 if self.clock_source().startswith("INT") else 2
953953
), # Internal | External
@@ -957,27 +957,27 @@ def generate_sequence_cfg(self) -> dict[str, float]:
957957
"EXTERNAL_REFERENCE_TYPE": 1, # Fixed | Variable
958958
"REFERENCE_CLOCK_FREQUENCY_SELECTION": 1,
959959
# 10 MHz | 20 MHz | 100 MHz
960-
"TRIGGER_SOURCE": 1 if self.get("trigger_source").startswith("EXT") else 2,
960+
"TRIGGER_SOURCE": 1 if self.trigger_source.get().startswith("EXT") else 2,
961961
# External | Internal
962962
"TRIGGER_INPUT_IMPEDANCE": (
963-
1 if self.get("trigger_impedance") == 50.0 else 2
963+
1 if self.trigger_impedance.get() == 50.0 else 2
964964
), # 50 ohm | 1 kohm
965965
"TRIGGER_INPUT_SLOPE": (
966-
1 if self.get("trigger_slope").startswith("POS") else 2
966+
1 if self.trigger_slope.get().startswith("POS") else 2
967967
), # Positive | Negative
968968
"TRIGGER_INPUT_POLARITY": (
969969
1 if self.ask("TRIGger:POLarity?").startswith("POS") else 2
970970
), # Positive | Negative
971-
"TRIGGER_INPUT_THRESHOLD": self.get("trigger_level"), # V
971+
"TRIGGER_INPUT_THRESHOLD": self.trigger_level.get(), # V
972972
"EVENT_INPUT_IMPEDANCE": (
973-
1 if self.get("event_impedance") == 50.0 else 2
973+
1 if self.event_impedance.get() == 50.0 else 2
974974
), # 50 ohm | 1 kohm
975975
"EVENT_INPUT_POLARITY": (
976-
1 if self.get("event_polarity").startswith("POS") else 2
976+
1 if self.event_polarity.get().startswith("POS") else 2
977977
), # Positive | Negative
978-
"EVENT_INPUT_THRESHOLD": self.get("event_level"), # V
978+
"EVENT_INPUT_THRESHOLD": self.event_level(), # V
979979
"JUMP_TIMING": (
980-
1 if self.get("event_jump_timing").startswith("SYNC") else 2
980+
1 if self.event_jump_timing.get().startswith("SYNC") else 2
981981
), # Sync | Async
982982
"RUN_MODE": 4, # Continuous | Triggered | Gated | Sequence
983983
"RUN_STATE": 0, # On | Off

tests/drivers/test_tektronix_AWG5014C.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import numpy as np
22
import pytest
33

4-
from qcodes.instrument_drivers.tektronix.AWG5014 import Tektronix_AWG5014
4+
from qcodes.instrument_drivers.tektronix import TektronixAWG5014
55

66

77
@pytest.fixture(scope="function")
88
def awg():
9-
awg_sim = Tektronix_AWG5014(
9+
awg_sim = TektronixAWG5014(
1010
"awg_sim",
1111
address="GPIB0::1::INSTR",
1212
timeout=1,

tests/test_instrument.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
)
3232
from qcodes.metadatable import Metadatable
3333
from qcodes.parameters import Function, Parameter
34+
from qcodes.utils import QCoDeSDeprecationWarning
3435

3536
if TYPE_CHECKING:
3637
from collections.abc import Iterator
@@ -273,12 +274,20 @@ def test_add_remove_f_p(testdummy) -> None:
273274
testdummy.add_function("dac1", call_cmd="foo")
274275

275276
# test custom __get_attr__ for functions
276-
fcn = testdummy["function"]
277-
assert isinstance(fcn, Function)
277+
with pytest.warns(
278+
QCoDeSDeprecationWarning,
279+
match="Use attributes directly on the instrument object instead",
280+
):
281+
fcn = testdummy["function"]
282+
assert isinstance(fcn, Function)
278283
# by design, one gets the parameter if a function exists
279284
# and has same name
280-
dac1 = testdummy["dac1"]
281-
assert isinstance(dac1, Parameter)
285+
with pytest.warns(
286+
QCoDeSDeprecationWarning,
287+
match="Use attributes directly on the instrument object instead",
288+
):
289+
dac1 = testdummy["dac1"]
290+
assert isinstance(dac1, Parameter)
282291

283292

284293
def test_instances(testdummy, parabola) -> None:

0 commit comments

Comments
 (0)