Skip to content

Commit 0c2cb21

Browse files
authored
Merge pull request #6212 from jenshnielsen/backport_warning_parameter
Backport #6211
2 parents 5b67917 + 86ab421 commit 0c2cb21

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

docs/changes/0.46.0.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ Breaking Changes:
2121
- It is now considered unsupported to modify the `parameters` attribute of an instrument or instrument module after it has been created.
2222
To remove a parameter from an instrument use the `remove_parameter` method. (:pr:`6174`)
2323

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

2730

2831
Improved:

src/qcodes/parameters/parameter_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,14 @@ def __init__(
334334
)
335335
if existing_parameter is None:
336336
existing_attribute = getattr(instrument, name, None)
337-
if existing_attribute is not None:
337+
if isinstance(existing_attribute, ParameterBase):
338338
raise KeyError(
339-
f"Parameter {name} overrides an attribute of the same name on instrument {instrument}"
339+
f"Duplicate parameter name {name} on instrument {instrument}"
340+
)
341+
elif existing_attribute is not None:
342+
warnings.warn(
343+
f"Parameter {name} overrides an attribute of the same name on instrument {instrument} "
344+
"This will be an error in the future.",
340345
)
341346

342347
instrument.parameters[name] = self

tests/parameter/test_parameter_override.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@ def __init__(self, name, **kwargs):
5959
def test_overriding_parameter_attribute_with_parameter_raises():
6060
with pytest.raises(
6161
KeyError,
62-
match="Parameter voltage overrides an attribute of the same name on instrument",
62+
match="Duplicate parameter name voltage on instrument",
6363
):
6464
DummyOverrideInstr("my_instr")
6565

6666

6767
def test_overriding_attribute_with_parameter_raises():
68-
with pytest.raises(
69-
KeyError,
68+
with pytest.warns(
7069
match="Parameter voltage overrides an attribute of the same name on instrument",
7170
):
7271
DummyParameterIsAttrInstr("my_instr")

0 commit comments

Comments
 (0)