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

Warn on a bad signature for an 'observe'-decorated function #1529

Merged
merged 6 commits into from
Sep 16, 2021

Conversation

mdickinson
Copy link
Member

@mdickinson mdickinson commented Sep 15, 2021

This PR adds a runtime warning for an observe-decorated method whose signature is incompatible with 'observe'.

I'm in two minds about whether this is a good idea - it's unnecessary runtime overhead, but on the other hand that runtime overhead should be small and will typically only apply at import time.

One the other hand, forgetting the event parameter is a very common mistake with observe-decorated methods, especially since the event-less signature is compatible with on_trait_change.

I haven't extended the fix to dynamic applications of observe: a.observe(handler, expression); that does seem like a case where we don't want the extra runtime overhead, since there may be many hidden calls to observe in a codebase, but occurrences of the observe decorator are limited in comparison.

Closes #1428.

Checklist

  • Tests

@mdickinson
Copy link
Member Author

This caught a bug in Traits' own test suite, here:

def handler(event):
pass

Given that, I'm inclined to keep the warning.

@mdickinson
Copy link
Member Author

This caught a bug in Traits' own test suite

Fixed in #1530.

Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

LGTM with one question/request for clarification

traits/has_traits.py Outdated Show resolved Hide resolved
@mdickinson mdickinson merged commit 9f93433 into main Sep 16, 2021
@mdickinson mdickinson deleted the feature/warn-on-bad-observe-signature branch September 16, 2021 12:36
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.

Warn if "observe" decorator applied to method with only "self"
2 participants