-
Notifications
You must be signed in to change notification settings - Fork 85
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
Conversation
@@ -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) | |||
|
There was a problem hiding this comment.
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 Report
@@ 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
Continue to review full report at Codecov.
|
… observer should not be mutated despite this change
|
||
Parameters | ||
---------- | ||
match_func : callable(str, TraitType) -> boolean |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick:
match_func : callable(str, TraitType) -> boolean | |
match_func : callable(str, TraitType) -> bool |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick:
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 ...
There was a problem hiding this comment.
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...
There was a problem hiding this 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 Report
@@ 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
Continue to review full report at Codecov.
|
This PR implements item 11 of #977
TraitAddedObserver
for supporting other observers that need to handletrait_added
. - UseTraitAddedObserver
inNamedTraitObserver
.Checklist
Update API reference (this observer is for internal use.docs/source/traits_api_reference
)Update User manual (docs/source/traits_user_manual
)Update type annotation hints intraits-stubs