Skip to content

Commit

Permalink
Add source validators to delegate parameter validator
Browse files Browse the repository at this point in the history
This will ensure that when the validator is used to find the datatype of a parameter it can be detected correctly
  • Loading branch information
jenshnielsen committed Jan 27, 2025
1 parent ad7d762 commit 23591b1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/qcodes/parameters/delegate_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from collections.abc import Sequence
from datetime import datetime

from qcodes.validators.validators import Validator

from .parameter_base import ParamDataType, ParamRawDataType


Expand Down Expand Up @@ -314,3 +316,18 @@ def validate(self, value: ParamDataType) -> None:
super().validate(value)
if self.source is not None:
self.source.validate(self._from_value_to_raw_value(value))

@property
def validators(self) -> tuple[Validator, ...]:
"""
Tuple of all validators associated with the parameter. Note that this
includes validators of the source parameter if source parameter is set
and has any validators.
:getter: All validators associated with the parameter.
"""
source_validators: tuple[Validator, ...] = (
self.source.validators if self.source is not None else ()
)

return source_validators + tuple(self._vals)
5 changes: 3 additions & 2 deletions src/qcodes/parameters/parameter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,10 @@ def vals(self) -> Validator | None:
RuntimeError: If removing the first validator when more than one validator is set.
"""
validators = self.validators

if len(self._vals):
return self._vals[0]
if len(validators):
return validators[0]
else:
return None

Expand Down
13 changes: 13 additions & 0 deletions tests/parameter/test_delegate_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,19 @@ def test_value_validation() -> None:
delegate_param.validate(11)


def test_validator_delegates_as_expected() -> None:
source_param = Parameter("source", set_cmd=None, get_cmd=None)
delegate_param = DelegateParameter("delegate", source=source_param)
some_validator = vals.Numbers(-10, 10)
source_param.vals = some_validator
delegate_param.vals = None
delegate_param.validate(1)
with pytest.raises(ValueError):
delegate_param.validate(11)
assert delegate_param.validators == (some_validator,)
assert delegate_param.vals == some_validator


def test_value_validation_with_offset_and_scale() -> None:
source_param = Parameter(
"source", set_cmd=None, get_cmd=None, vals=vals.Numbers(-5, 5)
Expand Down

0 comments on commit 23591b1

Please sign in to comment.