From eb82f688b7152aa6c5aa3c8c2da946187555bf6e Mon Sep 17 00:00:00 2001 From: Tobias Hangleiter Date: Fri, 17 May 2024 16:31:52 +0200 Subject: [PATCH 1/4] Test setpoints and basis types in register_parameter --- src/qcodes/dataset/measurements.py | 18 +++++++++++++ .../test_measurement_context_manager.py | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/qcodes/dataset/measurements.py b/src/qcodes/dataset/measurements.py index 5f97d616437..3e779db9e11 100644 --- a/src/qcodes/dataset/measurements.py +++ b/src/qcodes/dataset/measurements.py @@ -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): @@ -970,6 +976,18 @@ def register_parameter( return self + @staticmethod + def _check_setpoints_type(arg: setpoints_type, name: str): + 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: """ diff --git a/tests/dataset/measurement/test_measurement_context_manager.py b/tests/dataset/measurement/test_measurement_context_manager.py index 34a8f35b707..6027e796dfb 100644 --- a/tests/dataset/measurement/test_measurement_context_manager.py +++ b/tests/dataset/measurement/test_measurement_context_manager.py @@ -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)) + + with pytest.raises(TypeError): + meas.register_parameter(DMM.v1, setpoints=(DAC.ch1, 3)) + + def test_register_parameter_numbers(DAC, DMM) -> None: """ Test the registration of scalar QCoDeS parameters From 97d6596d353b6d119787e299d1c08162c2c334b7 Mon Sep 17 00:00:00 2001 From: Tobias Hangleiter Date: Fri, 17 May 2024 16:36:34 +0200 Subject: [PATCH 2/4] Add newsfragment --- docs/changes/newsfragments/6084.improved | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/newsfragments/6084.improved diff --git a/docs/changes/newsfragments/6084.improved b/docs/changes/newsfragments/6084.improved new file mode 100644 index 00000000000..94f99c0d4e4 --- /dev/null +++ b/docs/changes/newsfragments/6084.improved @@ -0,0 +1 @@ +Measurement.register_parameter() now tests for correct types of arguments setpoints and basis. From 5cbc8e43fadbaa0bd8d80a3c1f6c64916d5dbe27 Mon Sep 17 00:00:00 2001 From: Tobias Hangleiter Date: Fri, 17 May 2024 17:43:45 +0200 Subject: [PATCH 3/4] Add mypy ignore directive --- tests/dataset/measurement/test_measurement_context_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/dataset/measurement/test_measurement_context_manager.py b/tests/dataset/measurement/test_measurement_context_manager.py index 6027e796dfb..19f8133a6ba 100644 --- a/tests/dataset/measurement/test_measurement_context_manager.py +++ b/tests/dataset/measurement/test_measurement_context_manager.py @@ -70,10 +70,10 @@ def test_register_parameter_arg_types(DAC, DMM): meas.register_parameter(DMM.v1, setpoints="foo") with pytest.raises(TypeError): - meas.register_parameter(DMM.v1, basis=(DAC.ch1, 3)) + 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)) + meas.register_parameter(DMM.v1, setpoints=(DAC.ch1, 3)) # type: ignore[arg-type] def test_register_parameter_numbers(DAC, DMM) -> None: From be97efea6f645cb4f574f52b754fd526c3905ce8 Mon Sep 17 00:00:00 2001 From: Tobias Hangleiter Date: Fri, 17 May 2024 19:35:02 +0200 Subject: [PATCH 4/4] Add return type annotation --- src/qcodes/dataset/measurements.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qcodes/dataset/measurements.py b/src/qcodes/dataset/measurements.py index 3e779db9e11..b61b28b94e2 100644 --- a/src/qcodes/dataset/measurements.py +++ b/src/qcodes/dataset/measurements.py @@ -977,7 +977,7 @@ def register_parameter( return self @staticmethod - def _check_setpoints_type(arg: setpoints_type, name: str): + def _check_setpoints_type(arg: setpoints_type, name: str) -> None: if ( not isinstance(arg, Sequence) or isinstance(arg, str)