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

Fix TraitListEvents for slice assignments with step != 1. #300

Closed
mdickinson opened this issue Jun 6, 2016 · 1 comment
Closed

Fix TraitListEvents for slice assignments with step != 1. #300

mdickinson opened this issue Jun 6, 2016 · 1 comment

Comments

@mdickinson
Copy link
Member

Slice assignments with a step != 1 currently emit a TraitListEvent whose attributes are wrapped in an extra list:

>>> from traits.api import *
>>> class A(HasTraits): foo = List(Int)
... 
>>> def foo_items_listener(new): print "List event: ", new.added, new.removed
... 
>>> a = A(foo=range(5)); a.on_trait_change(foo_items_listener, 'foo_items')
>>> a.foo[:] = range(4)
List event:  [0, 1, 2, 3] [0, 1, 2, 3, 4]
>>> a.foo[::2] = [23, 47]
List event:  [[23, 47]] [[0, 2]]

There are applications that make use of this behaviour, so we've deliberately avoided 'fixing' it so far. For Traits 5.0, it may be worth fixing it (with a corresponding noisy entry in the changelog, of course).

Code:

if key.step == 1:
# FIXME: Bug-for-bug compatibility with old __setslice__ code.
# In this case, we return a TraitListEvent with an
# index=key.start and the removed and added lists as they
# are.
index = key.start
else:
# Otherwise, we have an extended slice which was handled,
# badly, by __setitem__ before. In this case, we return the
# removed and added lists wrapped in another list.
index = key
values = [values]
removed = [removed]

@mdickinson mdickinson added this to the 5.0.0 release milestone Jun 6, 2016
@rahulporuri rahulporuri removed this from the 5.0.0 release milestone Mar 12, 2019
@mdickinson
Copy link
Member Author

This was re-reported in #742, and fixed in #771

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

No branches or pull requests

3 participants