Skip to content

Commit

Permalink
Merge pull request #6211 from jenshnielsen/only_warn_on_attr
Browse files Browse the repository at this point in the history
Only warn on parameter overriding attribute if this is not a parameter
  • Loading branch information
jenshnielsen committed Jun 28, 2024
1 parent 5b67917 commit 86ab421
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions docs/changes/0.46.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ Breaking Changes:
- It is now considered unsupported to modify the `parameters` attribute of an instrument or instrument module after it has been created.
To remove a parameter from an instrument use the `remove_parameter` method. (:pr:`6174`)

- InstrumentBase.add_parameter will now error if an attribute of the same name as the parameter added already exists. This
is similar to how it would error if a parameter of the same name existed. (:pr:`6174`)
- InstrumentBase.add_parameter will now error if an attribute of the same name as the parameter added already exists and
this attribute is an instance of `ParameterBase`. This is to prevent issues where a parameter is partially
overwritten by a new parameter. To remove the existing Parameter use the new `instrument.remove_parameter`` method.
If the attribute is not a ParameterBase this will instead warn. It is the intention that this becomes an error in the future.
(:pr:`6174`) (:pr:`6211`)


Improved:
Expand Down
9 changes: 7 additions & 2 deletions src/qcodes/parameters/parameter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,14 @@ def __init__(
)
if existing_parameter is None:
existing_attribute = getattr(instrument, name, None)
if existing_attribute is not None:
if isinstance(existing_attribute, ParameterBase):
raise KeyError(
f"Parameter {name} overrides an attribute of the same name on instrument {instrument}"
f"Duplicate parameter name {name} on instrument {instrument}"
)
elif existing_attribute is not None:
warnings.warn(
f"Parameter {name} overrides an attribute of the same name on instrument {instrument} "
"This will be an error in the future.",
)

instrument.parameters[name] = self
Expand Down
5 changes: 2 additions & 3 deletions tests/parameter/test_parameter_override.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ def __init__(self, name, **kwargs):
def test_overriding_parameter_attribute_with_parameter_raises():
with pytest.raises(
KeyError,
match="Parameter voltage overrides an attribute of the same name on instrument",
match="Duplicate parameter name voltage on instrument",
):
DummyOverrideInstr("my_instr")


def test_overriding_attribute_with_parameter_raises():
with pytest.raises(
KeyError,
with pytest.warns(
match="Parameter voltage overrides an attribute of the same name on instrument",
):
DummyParameterIsAttrInstr("my_instr")
Expand Down

0 comments on commit 86ab421

Please sign in to comment.