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

Make indexing and slicing consistent with Python. #1183

Merged
merged 1 commit into from
Feb 7, 2017
Merged

Conversation

jgosmann
Copy link
Collaborator

@jgosmann jgosmann commented Oct 31, 2016

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?

  • Quick (less than 40 lines changed or changes are straightforward)

Types of changes:

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that causes existing functionality to change)

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:

  • I have read the CONTRIBUTING.rst document.
  • [na] I have updated the documentation accordingly.
  • I have included a changelog entry.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@Seanny123
Copy link
Contributor

LGTM.

Copy link
Member

@tbekolay tbekolay left a comment

Choose a reason for hiding this comment

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

I initially read the changelog as all integer indexing raises an IndexError, so I made a fixup to clarify that only out of range access raise the error. Aside from that, PR looks great, merging!

Integer indices out of bounds should raises an IndexError, whereas
slices should get truncated silently.

This has the added benefit of allowing iteration over Nengo objects.

Fixes #1176.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants