Make indexing and slicing consistent with Python. #1183
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and context:
Refer to the discussion in #1176. Basically, Nengo was converting integer indices on Nengo objects into slice and thus no
IndexError
was raised for out of bound indices. One particular problem with this behaviour was that iteration over Nengo objects would produce an infinite loop.With this PR integer indexing will raise the appropriate
IndexError
and slice indexing will truncate to match default Python behaviour. There is also array indexing which will behave like integer indexing. This changes make the iteration example from #1176 work without producing an inifinite loop.How has this been tested?
Added a few more test cases to check out of range indices and corner cases like negative indices. Also run the infinite loop example from #1176 which did not produce an infinite loop anymore.
How long should this take to review?
Types of changes:
Not entirely sure. I think of it as a bug fix, but in theory someone could have relied on the old truncating behaviour of integer indices, so the change could break existing models.
Checklist: