Skip to content

Commit

Permalink
Merge pull request #6084 from qutech/enh/register_parameter_type_check
Browse files Browse the repository at this point in the history
Test setpoints and basis types in register_parameter
  • Loading branch information
jenshnielsen authored May 17, 2024
2 parents 239e936 + be97efe commit 48804ca
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changes/newsfragments/6084.improved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Measurement.register_parameter() now tests for correct types of arguments setpoints and basis.
18 changes: 18 additions & 0 deletions src/qcodes/dataset/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,12 @@ def register_parameter(
f"{ParamSpec.allowed_types} are supported."
)

if setpoints is not None:
self._check_setpoints_type(setpoints, "setpoints")

if basis is not None:
self._check_setpoints_type(basis, "basis")

if isinstance(parameter, ArrayParameter):
self._register_arrayparameter(parameter, setpoints, basis, paramtype)
elif isinstance(parameter, ParameterWithSetpoints):
Expand Down Expand Up @@ -970,6 +976,18 @@ def register_parameter(

return self

@staticmethod
def _check_setpoints_type(arg: setpoints_type, name: str) -> None:
if (
not isinstance(arg, Sequence)
or isinstance(arg, str)
or any(not isinstance(a, (str, ParameterBase)) for a in arg)
):
raise TypeError(
f"{name} should be a sequence of str or ParameterBase, not "
f"{type(arg)}"
)

@staticmethod
def _infer_paramtype(parameter: ParameterBase, paramtype: str | None) -> str | None:
"""
Expand Down
25 changes: 25 additions & 0 deletions tests/dataset/measurement/test_measurement_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ def test_log_includes_extra_info(
assert "some extra info" in caplog.text


@pytest.mark.usefixtures("experiment")
def test_register_parameter_arg_types(DAC, DMM):
"""Test basis and setpoints argument types."""
meas = Measurement()
meas.register_parameter(DAC.ch1)

with pytest.raises(TypeError):
meas.register_parameter(DMM.v1, basis=DAC.ch1)

with pytest.raises(TypeError):
meas.register_parameter(DMM.v1, setpoints=DAC.ch1)

with pytest.raises(TypeError):
meas.register_parameter(DMM.v1, basis="foo")

with pytest.raises(TypeError):
meas.register_parameter(DMM.v1, setpoints="foo")

with pytest.raises(TypeError):
meas.register_parameter(DMM.v1, basis=(DAC.ch1, 3)) # type: ignore[arg-type]

with pytest.raises(TypeError):
meas.register_parameter(DMM.v1, setpoints=(DAC.ch1, 3)) # type: ignore[arg-type]


def test_register_parameter_numbers(DAC, DMM) -> None:
"""
Test the registration of scalar QCoDeS parameters
Expand Down

0 comments on commit 48804ca

Please sign in to comment.