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 delegate parameter warning #6722

Merged
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
3 changes: 3 additions & 0 deletions docs/changes/newsfragments/6722.improved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When passing ``bind_to_instrument=False`` to ``InstrumentBase.add_parameter`` a warning that
recommends not doing this is now raised suggesting a better alternative.
This replaces an existing inconsistent warning which was misleading.
12 changes: 11 additions & 1 deletion src/qcodes/instrument/instrument_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ def add_parameter(
if "bind_to_instrument" not in kwargs.keys():
kwargs["bind_to_instrument"] = True

bind_to_instrument = kwargs["bind_to_instrument"]

if bind_to_instrument is False:
warnings.warn(
f"Parameter {name} passed to `add_parameter` "
"on instrument {self.full_name} with `bind_to_instrument=False`. "
"This is not recommended as it results in inconsistent behavior. "
"To disable snapshotting of the parameter set `snapshot_exclude=True`."
)

try:
param = parameter_class(name=name, instrument=self, **kwargs)
except TypeError:
Expand All @@ -180,7 +190,7 @@ def add_parameter(
param = parameter_class(name=name, instrument=self, **kwargs)

existing_parameter = self.parameters.get(name, None)
if not existing_parameter:
if not existing_parameter and bind_to_instrument:
warnings.warn(
f"Parameter {name} did not correctly register itself on instrument"
f" {self.name}. Please check that `instrument` argument is passed "
Expand Down
13 changes: 13 additions & 0 deletions tests/parameter/test_parameter_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,16 @@ def test_parameter_registration_with_non_kwargs_passing_parameter(
# (bind_to_instrument specifically)
# to the baseclass it will still be registered on the instr
assert "brokenparameter2" in dummy_attr_instr.parameters.keys()


def test_parameter_registration_bind_to_instrument_false(
dummy_attr_instr: DummyAttrInstrument,
) -> None:
with pytest.warns(UserWarning, match="passed to `add_parameter`"):
dummy_attr_instr.add_parameter(
name="non_binding_parameter",
set_cmd=None,
get_cmd=None,
bind_to_instrument=False,
)
assert "non_binding_parameter" not in dummy_attr_instr.parameters.keys()
Loading