Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: DelegateParameter.validate also validates the source parameter #4870

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changes/newsfragments/4870.improved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``validate`` method of ``DelegateParameter`` now also validates the given value
against the validator of the source parameter (if source parameter is present).
17 changes: 17 additions & 0 deletions qcodes/parameters/delegate_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,20 @@ def snapshot_base(
)
snapshot.update({"source_parameter": source_parameter_snapshot})
return snapshot

def validate(self, value: ParamDataType) -> None:
"""
Validate the supplied value.
If it has a source parameter, validate the value as well with the source validator.

Args:
value: value to validate

Raises:
TypeError: If the value is of the wrong type.
ValueError: If the value is outside the bounds specified by the
validator.
"""
super().validate(value)
if self.source is not None:
self.source.validate(self._from_value_to_raw_value(value))
70 changes: 70 additions & 0 deletions qcodes/tests/parameter/test_delegate_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
from hypothesis import given

import qcodes.validators as vals
from qcodes.parameters import DelegateParameter, Parameter, ParamRawDataType

from .conftest import BetterGettableParam
Expand Down Expand Up @@ -512,3 +513,72 @@ def test_underlying_instrument_property_for_delegate_parameter():

d = DelegateParameter('delegate_parameter_without_source', source=None)
assert d.underlying_instrument is None


def test_value_validation():
source_param = Parameter("source", set_cmd=None, get_cmd=None)
delegate_param = DelegateParameter("delegate", source=source_param)

delegate_param.vals = vals.Numbers(-10, 10)
source_param.vals = None
delegate_param.validate(1)
with pytest.raises(ValueError):
delegate_param.validate(11)

delegate_param.vals = None
source_param.vals = vals.Numbers(-5, 5)
delegate_param.validate(1)
with pytest.raises(ValueError):
delegate_param.validate(6)

delegate_param.vals = vals.Numbers(-10, 10)
source_param.vals = vals.Numbers(-5, 5)
delegate_param.validate(1)
with pytest.raises(ValueError):
delegate_param.validate(6)
with pytest.raises(ValueError):
delegate_param.validate(11)
jenshnielsen marked this conversation as resolved.
Show resolved Hide resolved


def test_value_validation_with_offset_and_scale():
source_param = Parameter(
"source", set_cmd=None, get_cmd=None, vals=vals.Numbers(-5, 5)
)
delegate_param = DelegateParameter(
"delegate", source=source_param, vals=vals.Numbers(-10, 10)
)

source_param.offset = 100
source_param.scale = None
source_param.validate(0) # raw_value = 100
source_param.set(0)
delegate_param.validate(0) # raw_value = 0
delegate_param.set(0)

source_param.offset = None
source_param.scale = 100
source_param.validate(1) # raw_value = 100
source_param.set(1)
delegate_param.validate(1) # raw_value = 1
delegate_param.set(1)

source_param.offset = None
source_param.scale = None

delegate_param.offset = 100
delegate_param.scale = None
source_param.validate(0) # raw_value = 0
source_param.set(0)
with pytest.raises(ValueError):
delegate_param.validate(0) # raw_value = 100
with pytest.raises(ValueError):
delegate_param.set(0)

delegate_param.offset = None
delegate_param.scale = 100
source_param.validate(1) # raw_value = 1
source_param.set(1)
with pytest.raises(ValueError):
delegate_param.validate(1) # raw_value = 100
with pytest.raises(ValueError):
delegate_param.set(1)