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

Add origin to parameter warnings #1481

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
16 changes: 10 additions & 6 deletions qcodes/instrument/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,20 @@ def __init__(self, name: str,
if hasattr(self, 'get_raw') and not getattr(self.get_raw, '__qcodes_is_abstract_method__', False):
self.get = self._wrap_get(self.get_raw)
elif hasattr(self, 'get'):
warnings.warn('Wrapping get method, original get method will not '
'be directly accessible. It is recommended to '
'define get_raw in your subclass instead.')
warnings.warn(f'Wrapping get method of parameter: {self.full_name},'
f' original get method will not '
f'be directly accessible. It is recommended to '
f'define get_raw in your subclass instead. '
f'Overwriting get will be an error in the future.')
self.get = self._wrap_get(self.get)
if hasattr(self, 'set_raw') and not getattr(self.set_raw, '__qcodes_is_abstract_method__', False):
self.set = self._wrap_set(self.set_raw)
elif hasattr(self, 'set'):
warnings.warn('Wrapping set method, original set method will not '
'be directly accessible. It is recommended to '
'define set_raw in your subclass instead.')
warnings.warn(f'Wrapping set method of parameter: {self.full_name}, '
f'original set method will not '
f'be directly accessible. It is recommended to '
f'define set_raw in your subclass instead. '
f'Overwriting set will be an error in the future.')
self.set = self._wrap_set(self.set)

# subclasses should extend this list with extra attributes they
Expand Down
58 changes: 58 additions & 0 deletions qcodes/tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def get_raw(self):
return 42


class DeprecatedParam(Parameter):
""" Parameter that uses deprecated wrapping of get and set"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._value = 42
self.set_count = 0
self.get_count = 0

def get(self):
self.get_count += 1
return self._value

def set(self, value):
self.set_count += 1
self._value = value


class BookkeepingValidator(vals.Validator):
"""
Validator that keeps track of what it validates
Expand Down Expand Up @@ -1189,3 +1206,44 @@ def test_context(self):
with self.instrument.a.set_to(3):
assert self.instrument.a.get() == 3
assert self.instrument.a.get() == 2


def test_deprecated_param_warns():
"""
Test that creating a parameter that has deprecated get and set still works
but raises the correct warnings.
"""

with pytest.warns(UserWarning) as record:
a = DeprecatedParam(name='foo')
assert len(record) == 2
assert record[0].message.args[0] == ("Wrapping get method of parameter: "
"foo, original get method will not be "
"directly accessible. It is "
"recommended to define get_raw in "
"your subclass instead. Overwriting "
"get will be an error in the future.")
assert record[1].message.args[0] == ("Wrapping set method of parameter: "
"foo, original set method will not be "
"directly accessible. It is "
"recommended to define set_raw in "
"your subclass instead. Overwriting "
"set will be an error in the future.")
# test that get and set are called as expected (not shadowed by wrapper)
assert a.get_count == 0
assert a.set_count == 0
assert a.get() == 42
assert a.get_count == 1
assert a.set_count == 0
a.set(11)
assert a.get_count == 1
assert a.set_count == 1
assert a.get() == 11
assert a.get_count == 2
assert a.set_count == 1
# check that wrapper functionality works e.g stepping is performed correctly
a.step = 1
a.set(20)
assert a.set_count == 1+9
assert a.get() == 20
assert a.get_count == 3