Remove "changed" from TraitDict change event
- Dominant language
- Python
- Stars
- 462
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
Currently when a `TraitDictObject` is mutated, the change event contains the following:
https://github.com/enthought/traits/blob/99d83bd8134bcb97b991724fde7bb5d6a24962de/traits/trait_dict_object.py#L38-L45
Instead of having these three mutually exclusive sets of values, we could just have two sets: `added` and `removed`. Keys that are found in both `added` and `removed` represent changed items. I.e. something like:
```
added : dict
Keys for added or updated items. Values are current.
removed : dict
Keys for removed or updated items. Values are no longer in the dict.
```
This API will likely result in cleaner change handler code. For example, the block for `event.changed` in the following example from [mayavi](https://github.com/enthought/mayavi/blob/749199d6924d6228e40d6391575af45647915226/tvtk/pyface/ui/qt4/actor_editor.py#L145-L157) can be removed:
```
try:
for obj, actors in event.removed.items():
self._remove_actors_widgets(actors)
for obj, actors in event.added.items():
self._add_actors_widgets(actors)
for obj, actors in event.changed.items():
# The actors in the event are the old ones. Grab the new ones
# from the actor map itself.
self._remove_actors_widgets(actors)
self._add_actors_widgets(self.value[obj])
finally:
scene.disable_render = old_disable_render
scene.render()
```
There are also instances where the meaning of `changed` was misinterpreted, from [traitsui](https://github.com/enthought/traitsui/blob/3e2ce8604f8916ac68025c78fdc1985001d9c18b/traitsui/qt4/styled_date_editor.py#L49-L60):
```
# Handle the added and changed items
groups_to_set = event.added
groups_to_set.update(event.changed)
styles = getattr(self.object, self.factory.styles_trait, None)
self._apply_styles(styles, groups_to_set)
# Handle the removed items by resetting them
[
list(map(self._reset_formatting, dates))
for dates in event.removed.values()
]
```
For backward compatibility, handlers using `on_trait_change` can continue receiving `added`, `removed` and `changed` unchanged.
For the observe framework (see #977), it is a new framework that users can opt-in when they are ready. This presents an opportunity to improve the API.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.