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

Add trait_added support for observing a named trait #1077

Merged
merged 16 commits into from
May 19, 2020

Conversation

kitchoi
Copy link
Contributor

@kitchoi kitchoi commented May 12, 2020

This PR implements item 11 of #977

  • Add an TraitAddedObserver for supporting other observers that need to handle trait_added. - Use TraitAddedObserver in NamedTraitObserver.

Checklist

  • Tests
  • Update API reference (docs/source/traits_api_reference) this observer is for internal use.
  • Update User manual (docs/source/traits_user_manual)
  • Update type annotation hints in traits-stubs

@@ -47,9 +51,6 @@ def call_add_or_remove_notifiers(**kwargs):
**kwargs
New argument values to use instead.
"""
def dispatch_same(handler, event):
handler(event)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For tests trying to remove notifiers, notifiers with different dispatcher are considered different. Instead of keep supplying a locally defined dispatcher to maintain equality, it is convenient to just use a constant one.

@codecov-io
Copy link

codecov-io commented May 12, 2020

Codecov Report

Merging #1077 into master will decrease coverage by 2.12%.
The diff coverage is 62.24%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1077      +/-   ##
==========================================
- Coverage   76.15%   74.03%   -2.13%     
==========================================
  Files          54       76      +22     
  Lines        6493     8415    +1922     
  Branches     1263     1597     +334     
==========================================
+ Hits         4945     6230    +1285     
- Misses       1205     1807     +602     
- Partials      343      378      +35     
Impacted Files Coverage Δ
traits/api.py 100.00% <ø> (+9.67%) ⬆️
traits/base_trait_handler.py 61.76% <ø> (ø)
traits/ctrait.py 71.54% <ø> (+0.47%) ⬆️
traits/has_traits.py 72.49% <ø> (-0.28%) ⬇️
traits/observers/_i_notifier.py 0.00% <0.00%> (ø)
traits/observers/events.py 0.00% <0.00%> (ø)
traits/traits.py 75.10% <ø> (-2.45%) ⬇️
traits/util/resource.py 15.25% <ø> (ø)
traits/observers/_generated_parser.py 51.96% <51.96%> (ø)
traits/trait_types.py 72.29% <80.00%> (-0.16%) ⬇️
... and 49 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 240d469...3eb6027. Read the comment docs.


Parameters
----------
match_func : callable(str, TraitType) -> boolean
Copy link
Member

Choose a reason for hiding this comment

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

TraitType doesn't seem like the right thing here. (We haven't got rid of TraitHandler yet, so not all of our traits even have an associated TraitType.)

I think I'd expect to receive the actual trait (so an instance of CTrait) instead.

Copy link
Member

Choose a reason for hiding this comment

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

Nitpick:

Suggested change
match_func : callable(str, TraitType) -> boolean
match_func : callable(str, TraitType) -> bool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This is a mistake indeed. Fixed.

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.

Mostly LGTM; a couple of nitpicks, and one point of clarification on TraitType versus CTrait needed.


Parameters
----------
match_func : callable(str, TraitType) -> boolean
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick:

Suggested change
match_func : callable(str, TraitType) -> boolean
match_func : callable(str, TraitType) -> bool

return
raise ValueError(
"Unable to observe 'trait_added' event on {!r}".format(object))
yield object._trait("trait_added", 2)
Copy link
Member

Choose a reason for hiding this comment

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

Those magic integers ("2") are killing me. But that's out of scope for this PR.

dispatcher=dispatcher,
)

def prevent_event(self, event):
Copy link
Member

Choose a reason for hiding this comment

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

The logical negation in this method implementation is making me wonder whether we should switch from prevent_event to accept_event to avoid the extra negative (both in the code, and conceptually). Are we seeing similar negations in other implementations of prevent_event?

That's orthogonal to this PR, of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most of the prevent_event are lambda event: False, a few of them are lambda event: event.old is Uninitialized. Switching this to accept_event would also look fine for those.

"""
object = event.object
name = event.new
trait = object.trait(name=name)
Copy link
Member

Choose a reason for hiding this comment

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

Ah, following on from my earlier comment, it looks as though we're passing a CTrait rather than TraitType here. I think the CTrait instance is what I'd expect.

return hash(self.return_value)


class TestTraitAddedObserverEqualHashImmutable(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

That's an impressive test class name ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm the "Immutable" part needs to go, removing...

@kitchoi kitchoi requested a review from mdickinson May 18, 2020 16:57
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

Co-authored-by: Mark Dickinson <mdickinson@enthought.com>
@codecov-commenter
Copy link

codecov-commenter commented May 19, 2020

Codecov Report

Merging #1077 into master will decrease coverage by 1.94%.
The diff coverage is 62.31%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1077      +/-   ##
==========================================
- Coverage   76.15%   74.21%   -1.95%     
==========================================
  Files          54       78      +24     
  Lines        6493     8469    +1976     
  Branches     1263     1610     +347     
==========================================
+ Hits         4945     6285    +1340     
- Misses       1205     1806     +601     
- Partials      343      378      +35     
Impacted Files Coverage Δ
traits/api.py 100.00% <ø> (+9.67%) ⬆️
traits/base_trait_handler.py 61.76% <ø> (ø)
traits/ctrait.py 72.35% <ø> (+1.28%) ⬆️
traits/observers/_i_notifier.py 0.00% <0.00%> (ø)
traits/observers/events.py 0.00% <0.00%> (ø)
traits/traits.py 75.10% <ø> (-2.45%) ⬇️
traits/util/resource.py 15.25% <ø> (ø)
traits/observers/_generated_parser.py 51.96% <51.96%> (ø)
traits/has_traits.py 73.51% <64.70%> (+0.74%) ⬆️
traits/trait_types.py 72.29% <80.00%> (-0.16%) ⬇️
... and 52 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 240d469...f6f1a7b. Read the comment docs.

@kitchoi kitchoi merged commit 486a289 into master May 19, 2020
@kitchoi kitchoi deleted the 977-trait-added-observer branch May 19, 2020 08:30
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.

4 participants