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

Alternative approach to handling equality comparison mode for notifications #1165

Merged
merged 13 commits into from
Jun 26, 2020

Conversation

kitchoi
Copy link
Contributor

@kitchoi kitchoi commented Jun 1, 2020

This PR is currently a draft for discussion purposes.

This PR presents an alternative approach to support observe even when the List, Set and Dict trait types have comparison mode set to equality.

The solution is to move the rich comparison from CTrait to the notifier, such that instead of silencing the notifications early in CTrait, the notifiers will do the equality check and silence the notification if applicable.

Pros:

Cons:

  • The equality check is done as many times as the number of notifiers attached to a trait. I hope this only presents a very slight performance cost.

Side-effect:

  • Previously post_setattr (if defined) is not called if the notifications are silenced due to equality check. Now post_setattr will be called even if change notifications are silenced due to equality check. The original behaviour looks to me an optimization that can be removed, but I am not entirely sure.

Uncertainty:

  • It looks as though the code path that reaches rich comparison in ctraits.c only applies to trait types with TraitType.trait. e.g. notifications emitted for Property and Delegate do not go through these equality checks. Filtering for TraitType.trait in the notifiers look enough, but I am not sure if there are other code paths using this rich comparison that have not been accounted for.

This approach may be an alternative to #1122.

If this works, it should present no visible changes to users of traits. However I think this is quite a substantial internal change that presents quite a bit of risk. Hence I'd be hesitant to let this into Traits 6.1 (it won't make it in anyway judging from the time line). It is perhaps reasonable for observe to warn about equality mode and leave CTrait/on_trait_change untouched in at least one release. (Edit: 6.1 is now out 🎉 )

Checklist

  • Tests
  • Update API reference (docs/source/traits_api_reference)
  • Update User manual (docs/source/traits_user_manual)
  • Update type annotation hints in traits-stubs

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

I think this is the right approach. A couple of minor comments.

traits/observation/_has_traits_helpers.py Show resolved Hide resolved
traits/observation/_has_traits_helpers.py Show resolved Hide resolved
traits/trait_notifiers.py Show resolved Hide resolved
traits/observation/tests/test_has_traits_helpers.py Outdated Show resolved Hide resolved
@kitchoi kitchoi requested a review from mdickinson June 26, 2020 13:18
@kitchoi
Copy link
Contributor Author

kitchoi commented Jun 26, 2020

Updated main comment: User manual is also updated now.

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

Changes look good, except that I think we simply want to update the documentation to match the Traits 6.2 behaviour rather than describing what happened in 6.1. (And a couple of tiny markup nitpicks.)

@mdickinson
Copy link
Member

I think we simply want to update the documentation to match the Traits 6.2 behaviour rather than describing what happened in 6.1.

We could possibly make use of the .. versionchanged:: ... markup to add a note about the change from 6.1 to 6.2, though.

kitchoi and others added 2 commits June 26, 2020 14:37
@kitchoi
Copy link
Contributor Author

kitchoi commented Jun 26, 2020

We could possibly make use of the .. versionchanged:: ... markup to add a note about the change from 6.1 to 6.2, though.

I have removed the section. Maybe the change can later live in the changelog / release notes?

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

LGTM! Waiting for CI ...

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

Successfully merging this pull request may close these issues.

2 participants