-
Notifications
You must be signed in to change notification settings - Fork 85
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
Further index normalization in TraitList #1009
Conversation
Here's an example showing the main effect: >>> from traits.api import HasTraits, List
>>> class A(HasTraits):
... foo = List()
... def _foo_items_changed(self, event):
... print(event.__dict__)
...
>>> a = A(foo=list(range(10)))
>>> a
<__main__.A object at 0x10d9d99f0>
>>> a.foo
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.foo[7::-2] = [10, 11, 12, 13] # note reversal of the 'index' below
{'index': slice(1, 8, 2), 'removed': [1, 3, 5, 7], 'added': [13, 12, 11, 10]} |
Codecov Report
@@ Coverage Diff @@
## master #1009 +/- ##
==========================================
+ Coverage 73.95% 74.13% +0.18%
==========================================
Files 51 51
Lines 6453 6460 +7
Branches 1277 1281 +4
==========================================
+ Hits 4772 4789 +17
+ Misses 1318 1305 -13
- Partials 363 366 +3
Continue to review full report at Codecov.
|
…slice-normalization
…slice-normalization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I am happy to see the exhaustive test. While it takes some time to run, it really isn't that long. It would be useful to keep them for it is so easy to get slice indices wrong.
Since the _normalize_slice_or_index
is a standalone function, it is possible to write a small unit test for it. I even tried setting the list length to be shorter than the maximum index. It is beyond our use case to have the length being too small, but the test passes:
def test_slices_equivalence(self):
length = 6
test_list = list(range(length))
# maximum index is larger than the list length, but that's okay
for test_slice in self.all_slices(max_index=10):
new_test_list = test_list[test_slice]
reversed, normalized_index = _normalize_slice_or_index(test_slice, length)
if isinstance(normalized_index, int):
normalized_index = slice(normalized_index, normalized_index + len(new_test_list))
else:
self.check_index_normalized(normalized_index, length)
new_list = test_list[normalized_index]
if reversed:
new_list = new_list[::-1]
self.assertEqual(new_test_list, new_list)
@kitchoi: Thanks for the review. A couple of followups:
Okey-dokey, let's do that.
Yes, I can do that. It makes me slightly twitchy to write unit tests for a private function, but I think we definitely don't want to elevate this function to public at this point: we don't want external users to start using it and relying on its behaviour. |
Pushed a change from this morning which updated the docstring of |
@@ -1095,7 +1095,7 @@ def test_trait_list_event(self): | |||
self.assertIs(self.last_event, old_event) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this PR be a good place to update the FIXME
comment in this test? It seems outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! I'll do that. It makes documenting the new behaviour all the more important, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and yes, the "wrapped in another list" bit is gone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
…slice-normalization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Resolving conflicts with master ... |
…slice-normalization
This PR builds on #1003 and:
__delitem__
and__setitem__
operations onTraitList
(index, removed, added)
triple.__delitem__
and__setitem__
operations. These tests are slow, so we may want to kill them at some point.The key part of the tests are these lines, which show how to reconstruct a list operation from the
(index, removed, added)
triple:traits/traits/tests/test_trait_list_object.py
Lines 1016 to 1029 in 9a74866