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

Station: fail hard when adding an Instrument with a name that is already registered #1714

Merged
merged 5 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 6 additions & 2 deletions qcodes/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import qcodes
from qcodes.utils.metadata import Metadatable
from qcodes.utils.helpers import (
make_unique, DelegateAttributes, YAML, checked_getattr)
DelegateAttributes, YAML, checked_getattr)

from qcodes.instrument.base import Instrument, InstrumentBase
from qcodes.instrument.parameter import (
Expand Down Expand Up @@ -184,7 +184,11 @@ def add_component(self, component: Metadatable, name: str = None,
if name is None:
name = getattr(component, 'name',
'component{}'.format(len(self.components)))
namestr = make_unique(str(name), self.components)
namestr = str(name)
if namestr in self.components.keys():
raise RuntimeError(
f'Cannot add component "{namestr}", because a '
'component of that name is already registered to the station')
self.components[namestr] = component
return namestr

Expand Down
13 changes: 1 addition & 12 deletions qcodes/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np

from qcodes.utils.helpers import (is_sequence, permissive_range, wait_secs,
make_unique, DelegateAttributes,
DelegateAttributes,
strip_attrs, full_class,
named_repr, make_sweep, is_sequence_of,
compare_dictionaries, NumpyJSONEncoder,
Expand Down Expand Up @@ -257,17 +257,6 @@ def test_warning(self):
self.assertEqual(logs.value.count('negative delay'), 1, logs.value)


class TestMakeUnique(TestCase):
def test_no_changes(self):
for s, existing in (('a', []), ('a', {}), ('a', ('A', ' a', 'a '))):
self.assertEqual(make_unique(s, existing), s)

def test_changes(self):
self.assertEqual(make_unique('a', ('a',)), 'a_2')
self.assertEqual(make_unique('a_2', ('a_2',)), 'a_2_2')
self.assertEqual(make_unique('a', ('a', 'a_2', 'a_3')), 'a_4')


class TestDelegateAttributes(TestCase):
def test_delegate_dict(self):
class ToDict(DelegateAttributes):
Expand Down
4 changes: 3 additions & 1 deletion qcodes/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ def __exit__(self, type, value, tb):
self.logger.addHandler(handler)


@deprecate(
reason=("Instrument names are cross process identifiers that should be "
Dominik-Vogel marked this conversation as resolved.
Show resolved Hide resolved
"chosen transparently."))
def make_unique(s, existing):
"""
make string s unique, able to be added to a sequence `existing` of
Expand All @@ -367,7 +370,6 @@ def make_unique(s, existing):

return s_out


Dominik-Vogel marked this conversation as resolved.
Show resolved Hide resolved
class DelegateAttributes:
"""
Mixin class to create attributes of this object by
Expand Down