Open kitchoi opened 4 years ago
Sorry for those receiving an email notification containing virtually no content. I hit some magic keybinding on GH which caused the issue to be submitted early. The issue is now updated.
The other part of this is that in the current TraitDictEvent
, we have incomplete information: the added
and removed
parts are complete, but for the changed
part, the event receiver gets the old values in the dict, but has to explicitly look up the new values. It's a surprising API that's easy to accidentally misuse (as the examples @kitchoi found show). The proposed change would mean that the event would contain complete information about the changes.
the event receiver gets the old values in the dict, but has to explicitly look up the new values
This has also always felt rather fragile to me: instead of information about the changes being generated when the changes happen, we look up those new values later. It's quite possible for the dictionary to have changed in the meantime (perhaps as the result of some other listener, or perhaps as the result of something horrible concurrency-related), so that the lookups fail.
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-L45Instead of having these three mutually exclusive sets of values, we could just have two sets:
added
andremoved
. Keys that are found in bothadded
andremoved
represent changed items. I.e. something like:This API will likely result in cleaner change handler code. For example, the block for
event.changed
in the following example from mayavi can be removed:There are also instances where the meaning of
changed
was misinterpreted, from traitsui:For backward compatibility, handlers using
on_trait_change
can continue receivingadded
,removed
andchanged
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.