Skip to content

Commit 50b8ebb

Browse files
authored
Merge pull request #7482 from jenshnielsen/b1500_types
Improve types in b1500
2 parents 5400240 + 17c2197 commit 50b8ebb

7 files changed

Lines changed: 205 additions & 137 deletions

File tree

src/qcodes/instrument_drivers/Keysight/keysightb1500/KeysightB1520A.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ def __init__(self, parent: "KeysightB1520A", name: str, **kwargs: Any):
12601260
super().__init__(parent=parent, name=name, **kwargs)
12611261
self._chnum = parent.channels[0]
12621262

1263-
self.add_submodule(
1263+
self.frequency_list = self.add_submodule(
12641264
"frequency_list",
12651265
KeysightB1500FrequencyList(self, "frequency_list", self._chnum),
12661266
)

tests/drivers/keysight_b1500/b1500_driver_tests/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import TYPE_CHECKING
12
from unittest.mock import MagicMock, PropertyMock
23

34
import pytest
@@ -8,9 +9,12 @@
89
KeysightB1500,
910
)
1011

12+
if TYPE_CHECKING:
13+
from collections.abc import Generator
14+
1115

1216
@pytest.fixture(name="b1500")
13-
def _make_b1500(request: FixtureRequest):
17+
def _make_b1500(request: FixtureRequest) -> "Generator[KeysightB1500, None, None]":
1418
request.addfinalizer(KeysightB1500.close_all)
1519

1620
try:
@@ -30,7 +34,7 @@ def _make_b1500(request: FixtureRequest):
3034

3135

3236
@pytest.fixture(name="mainframe")
33-
def _make_mainframe():
37+
def _make_mainframe() -> "Generator[MagicMock, None, None]":
3438
PropertyMock()
3539
mainframe = MagicMock()
3640
name_parts = PropertyMock(return_value=["mainframe"])

tests/drivers/keysight_b1500/b1500_driver_tests/test_b1500.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727

2828

29-
def test_make_module_from_model_name(mainframe) -> None:
29+
def test_make_module_from_model_name(mainframe: MagicMock) -> None:
3030
with pytest.raises(NotImplementedError):
3131
KeysightB1500.from_model_name(
3232
model="unsupported_module", slot_nr=0, parent=mainframe, name="dummy"
@@ -53,43 +53,50 @@ def test_make_module_from_model_name(mainframe) -> None:
5353
assert isinstance(smu, KeysightB1511B)
5454

5555

56-
def test_init(b1500) -> None:
56+
def test_init(b1500: KeysightB1500) -> None:
5757
assert hasattr(b1500, "smu1")
5858
assert hasattr(b1500, "smu2")
5959
assert hasattr(b1500, "cmu1")
6060
assert hasattr(b1500, "wgfmu1")
6161

6262

63-
def test_snapshot_does_not_raise_warnings(b1500) -> None:
63+
def test_snapshot_does_not_raise_warnings(b1500: KeysightB1500) -> None:
6464
with warnings.catch_warnings():
6565
warnings.simplefilter("error")
6666
b1500.snapshot(update=True)
6767

6868

69-
def test_submodule_access_by_class(b1500) -> None:
70-
assert b1500.smu1 in b1500.by_kind["SMU"]
71-
assert b1500.smu2 in b1500.by_kind["SMU"]
72-
assert b1500.cmu1 in b1500.by_kind["CMU"]
73-
assert b1500.wgfmu1 in b1500.by_kind["WGFMU"]
69+
def test_submodule_access_by_class(b1500: KeysightB1500) -> None:
70+
assert b1500.smu1 in b1500.by_kind[constants.ModuleKind.SMU]
71+
# while it does not type check it is possible to look up by string
72+
assert b1500.smu1 in b1500.by_kind["SMU"] # type: ignore
73+
assert b1500.smu1 in b1500.by_kind[constants.ModuleKind.SMU]
74+
assert b1500.smu2 in b1500.by_kind["SMU"] # type: ignore
75+
assert b1500.cmu1 in b1500.by_kind[constants.ModuleKind.CMU]
76+
assert b1500.cmu1 in b1500.by_kind["CMU"] # type: ignore
77+
assert b1500.wgfmu1 in b1500.by_kind[constants.ModuleKind.WGFMU]
78+
assert b1500.wgfmu1 in b1500.by_kind["WGFMU"] # type: ignore
7479

7580

76-
def test_submodule_access_by_slot(b1500) -> None:
81+
def test_submodule_access_by_slot(b1500: KeysightB1500) -> None:
7782
assert b1500.smu1 is b1500.by_slot[SlotNr.SLOT01]
7883
assert b1500.smu2 is b1500.by_slot[SlotNr.SLOT02]
79-
assert b1500.cmu1 is b1500.by_slot[3]
80-
assert b1500.wgfmu1 is b1500.by_slot[6]
84+
# while it does not type check it is possible to look up by integer
85+
assert b1500.cmu1 is b1500.by_slot[3] # type: ignore
86+
assert b1500.wgfmu1 is b1500.by_slot[6] # type: ignore
8187

8288

83-
def test_submodule_access_by_channel(b1500) -> None:
89+
def test_submodule_access_by_channel(b1500: KeysightB1500) -> None:
8490
assert b1500.smu1 is b1500.by_channel[ChNr.SLOT_01_CH1]
8591
assert b1500.smu2 is b1500.by_channel[ChNr.SLOT_02_CH1]
8692
assert b1500.cmu1 is b1500.by_channel[ChNr.SLOT_03_CH1]
8793
assert b1500.wgfmu1 is b1500.by_channel[ChNr.SLOT_06_CH1]
88-
assert b1500.wgfmu1 is b1500.by_channel[6]
94+
# while it does not type check it is possible to look up by integer
95+
assert b1500.wgfmu1 is b1500.by_channel[6] # type: ignore
8996
assert b1500.wgfmu1 is b1500.by_channel[ChNr.SLOT_06_CH2]
9097

9198

92-
def test_enable_multiple_channels(b1500) -> None:
99+
def test_enable_multiple_channels(b1500: KeysightB1500) -> None:
93100
mock_write = MagicMock()
94101
b1500.write = mock_write
95102

@@ -98,7 +105,7 @@ def test_enable_multiple_channels(b1500) -> None:
98105
mock_write.assert_called_once_with("CN 1,2,3")
99106

100107

101-
def test_disable_multiple_channels(b1500) -> None:
108+
def test_disable_multiple_channels(b1500: KeysightB1500) -> None:
102109
mock_write = MagicMock()
103110
b1500.write = mock_write
104111

@@ -107,7 +114,7 @@ def test_disable_multiple_channels(b1500) -> None:
107114
mock_write.assert_called_once_with("CL 1,2,3")
108115

109116

110-
def test_use_nplc_for_high_speed_adc(b1500) -> None:
117+
def test_use_nplc_for_high_speed_adc(b1500: KeysightB1500) -> None:
111118
mock_write = MagicMock()
112119
b1500.write = mock_write
113120

@@ -120,7 +127,7 @@ def test_use_nplc_for_high_speed_adc(b1500) -> None:
120127
mock_write.assert_called_once_with("AIT 0,2,3")
121128

122129

123-
def test_use_nplc_for_high_resolution_adc(b1500) -> None:
130+
def test_use_nplc_for_high_resolution_adc(b1500: KeysightB1500) -> None:
124131
mock_write = MagicMock()
125132
b1500.write = mock_write
126133

@@ -133,7 +140,7 @@ def test_use_nplc_for_high_resolution_adc(b1500) -> None:
133140
mock_write.assert_called_once_with("AIT 1,2,8")
134141

135142

136-
def test_autozero_enabled(b1500) -> None:
143+
def test_autozero_enabled(b1500: KeysightB1500) -> None:
137144
mock_write = MagicMock()
138145
b1500.write = mock_write
139146

@@ -150,7 +157,7 @@ def test_autozero_enabled(b1500) -> None:
150157
assert b1500.autozero_enabled() is False
151158

152159

153-
def test_use_manual_mode_for_high_speed_adc(b1500) -> None:
160+
def test_use_manual_mode_for_high_speed_adc(b1500: KeysightB1500) -> None:
154161
mock_write = MagicMock()
155162
b1500.write = mock_write
156163

@@ -168,7 +175,7 @@ def test_use_manual_mode_for_high_speed_adc(b1500) -> None:
168175
mock_write.assert_called_once_with("AIT 0,1,8")
169176

170177

171-
def test_self_calibration_successful(b1500) -> None:
178+
def test_self_calibration_successful(b1500: KeysightB1500) -> None:
172179
mock_ask = MagicMock()
173180
b1500.ask = mock_ask
174181

@@ -180,7 +187,7 @@ def test_self_calibration_successful(b1500) -> None:
180187
mock_ask.assert_called_once_with("*CAL?")
181188

182189

183-
def test_self_calibration_failed(b1500) -> None:
190+
def test_self_calibration_failed(b1500: KeysightB1500) -> None:
184191
mock_ask = MagicMock()
185192
b1500.ask = mock_ask
186193

@@ -193,12 +200,12 @@ def test_self_calibration_failed(b1500) -> None:
193200
mock_ask.assert_called_once_with("*CAL?")
194201

195202

196-
def test_error_message(b1500) -> None:
203+
def test_error_message(b1500: KeysightB1500) -> None:
197204
response = b1500.error_message()
198205
assert '+0,"No Error."' == response
199206

200207

201-
def test_clear_timer_count(b1500) -> None:
208+
def test_clear_timer_count(b1500: KeysightB1500) -> None:
202209
mock_write = MagicMock()
203210
b1500.write = mock_write
204211

@@ -211,15 +218,15 @@ def test_clear_timer_count(b1500) -> None:
211218
mock_write.assert_called_once_with("TSR 1")
212219

213220

214-
def test_set_measuremet_mode(b1500) -> None:
221+
def test_set_measuremet_mode(b1500: KeysightB1500) -> None:
215222
mock_write = MagicMock()
216223
b1500.write = mock_write
217224

218225
b1500.set_measurement_mode(mode=constants.MM.Mode.SPOT, channels=[1, 2])
219226
mock_write.assert_called_once_with("MM 1,1,2")
220227

221228

222-
def test_get_measurement_mode(b1500) -> None:
229+
def test_get_measurement_mode(b1500: KeysightB1500) -> None:
223230
mock_ask = MagicMock()
224231
b1500.ask = mock_ask
225232

@@ -229,7 +236,7 @@ def test_get_measurement_mode(b1500) -> None:
229236
assert measurement_mode["channels"] == [1, 2]
230237

231238

232-
def test_get_response_format_and_mode(b1500) -> None:
239+
def test_get_response_format_and_mode(b1500: KeysightB1500) -> None:
233240
mock_ask = MagicMock()
234241
b1500.ask = mock_ask
235242

@@ -239,7 +246,7 @@ def test_get_response_format_and_mode(b1500) -> None:
239246
assert measurement_mode["mode"] == constants.FMT.Mode(1)
240247

241248

242-
def test_enable_smu_filters(b1500) -> None:
249+
def test_enable_smu_filters(b1500: KeysightB1500) -> None:
243250
mock_write = MagicMock()
244251
b1500.write = mock_write
245252

@@ -271,7 +278,9 @@ def test_enable_smu_filters(b1500) -> None:
271278
mock_write.assert_called_once_with("FL 1,102,202,302")
272279

273280

274-
def test_error_message_is_called_after_setting_a_parameter(b1500) -> None:
281+
def test_error_message_is_called_after_setting_a_parameter(
282+
b1500: KeysightB1500,
283+
) -> None:
275284
mock_ask = MagicMock()
276285
b1500.ask = mock_ask
277286
mock_ask.return_value = '+0,"No Error."'

tests/drivers/keysight_b1500/b1500_driver_tests/test_b1511b_smu.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from typing import TYPE_CHECKING
23

34
import pytest
45

@@ -10,15 +11,21 @@
1011
KeysightB1511B,
1112
)
1213

14+
if TYPE_CHECKING:
15+
from collections.abc import Generator
16+
from unittest.mock import MagicMock
17+
1318

1419
@pytest.fixture(name="smu")
15-
def _make_smu(mainframe):
20+
def _make_smu(mainframe: "MagicMock") -> "Generator[KeysightB1511B, None, None]":
1621
slot_nr = 1
1722
smu = KeysightB1511B(parent=mainframe, name="B1511B", slot_nr=slot_nr)
1823
yield smu
1924

2025

21-
def test_force_invalid_current_output_range_when_asu_not_present(smu) -> None:
26+
def test_force_invalid_current_output_range_when_asu_not_present(
27+
smu: KeysightB1511B,
28+
) -> None:
2229
msg = re.escape("Invalid Source Current Output Range")
2330
with pytest.raises(RuntimeError, match=msg):
2431
smu.asu_present = True
@@ -27,7 +34,7 @@ def test_force_invalid_current_output_range_when_asu_not_present(smu) -> None:
2734

2835

2936
def test_i_measure_range_config_raises_invalid_range_error_when_asu_not_present(
30-
smu,
37+
smu: KeysightB1511B,
3138
) -> None:
3239
msg = re.escape("8 current measurement range")
3340
with pytest.raises(RuntimeError, match=msg):

0 commit comments

Comments
 (0)