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

Disallow repeated capability on session #883

Merged
merged 12 commits into from
Jun 4, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
* #### Added
* #### Changed
* #### Removed
* Explicitly disallow using a repeated capability on Session. `session[0].vertical_range = 1.0` will no longer work. Instead use `session.channels[0].vertical_range = 1.0` - [#853](https://github.com/ni/nimi-python/issues/853)
* ### NI-DMM
* #### Added
* #### Changed
Expand Down
7 changes: 7 additions & 0 deletions build/templates/session.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ constructor_params = helper.filter_parameters(init_function, helper.ParameterUsa
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? When you try to index a class that doesn't support it, the error is:

TypeError: 'Foo' object does not support indexing

We wouldn't want this to be different. Let's let Python do its thing.

If you think the text in the exception is a good enough reason (I'm not convinced) then let's make it really useful:

  • raise TypeError
  • text should say `'Session' object does not support indexing. XXX
  • XXX should be something that talks about the repeated capability containers for the class. The magic of codegen! (or we could at runtime figure it out)

Regardless, I'm not sure it's worth it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is needed. Without it, it does not error when using session[0]. I'm not sure it actually works, but it doesn't error.

  • Changed to TypeError
  • Text now says "'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}"
    • where {} is the list of containers supported by that driver

rep_caps = []
% for rep_cap in config['repeated_capabilities']:
rep_caps.append("${rep_cap['python_name']}")
% endfor
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
5 changes: 5 additions & 0 deletions generated/nidcpower/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,11 @@ def __setattr__(self, key, value):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
rep_caps = []
rep_caps.append("channels")
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
4 changes: 4 additions & 0 deletions generated/nidmm/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,10 @@ def __setattr__(self, key, value):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
rep_caps = []
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
5 changes: 5 additions & 0 deletions generated/nifake/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ def __setattr__(self, key, value):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
rep_caps = []
rep_caps.append("channels")
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
8 changes: 8 additions & 0 deletions generated/nifake/unit_tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,14 @@ def test_matcher_prints(self):
assert _matchers.CustomTypeMatcher(nifake.custom_struct, nifake.custom_struct(cs[0])).__repr__() == "CustomTypeMatcher(<class 'nifake.custom_struct.custom_struct'>, custom_struct(data=None, struct_int=42, struct_double=4.2))"
assert _matchers.CustomTypeBufferMatcher(nifake.custom_struct, cs_ctype).__repr__() == "CustomTypeBufferMatcher(<class 'nifake.custom_struct.custom_struct'>, [custom_struct(data=None, struct_int=42, struct_double=4.2), custom_struct(data=None, struct_int=43, struct_double=4.3), custom_struct(data=None, struct_int=42, struct_double=4.3)])"

def test_channel_on_session(self):
with nifake.Session('dev1') as session:
try:
session['100'].read_write_double = 5.0
assert False
except TypeError:
pass


# not a session test per se
def test_diagnostic_information():
Expand Down
7 changes: 7 additions & 0 deletions generated/nifgen/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,13 @@ def __setattr__(self, key, value):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
rep_caps = []
rep_caps.append("channels")
rep_caps.append("script_triggers")
rep_caps.append("markers")
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
5 changes: 5 additions & 0 deletions generated/niscope/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,11 @@ def __setattr__(self, key, value):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
rep_caps = []
rep_caps.append("channels")
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
5 changes: 5 additions & 0 deletions generated/niswitch/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ def __setattr__(self, key, value):
raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, key))
object.__setattr__(self, key, value)

def __getitem__(self, key):
rep_caps = []
rep_caps.append("channels")
raise TypeError("'Session' object does not support indexing. You should use the applicable repeated capabilities container(s): {}".format(', '.join(rep_caps)))

def _get_error_description(self, error_code):
'''_get_error_description

Expand Down
8 changes: 8 additions & 0 deletions src/nifake/unit_tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,14 @@ def test_matcher_prints(self):
assert _matchers.CustomTypeMatcher(nifake.custom_struct, nifake.custom_struct(cs[0])).__repr__() == "CustomTypeMatcher(<class 'nifake.custom_struct.custom_struct'>, custom_struct(data=None, struct_int=42, struct_double=4.2))"
assert _matchers.CustomTypeBufferMatcher(nifake.custom_struct, cs_ctype).__repr__() == "CustomTypeBufferMatcher(<class 'nifake.custom_struct.custom_struct'>, [custom_struct(data=None, struct_int=42, struct_double=4.2), custom_struct(data=None, struct_int=43, struct_double=4.3), custom_struct(data=None, struct_int=42, struct_double=4.3)])"

def test_channel_on_session(self):
with nifake.Session('dev1') as session:
try:
session['100'].read_write_double = 5.0
assert False
except TypeError:
pass


# not a session test per se
def test_diagnostic_information():
Expand Down