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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion traits/has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,31 @@ def observe_decorator(handler):
Parameters
----------
handler : callable
Method of a subclass of HasTraits
Method of a subclass of HasTraits, with signature of the form
``my_method(self, event)``.
"""
# Warn on a dubious handler signature. The handler should accept a call
# that passes a single positional argument (conventionally named
# "event") in addition to the usual "self".
try:
handler_signature = inspect.signature(handler)
except (ValueError, TypeError):
pass
mdickinson marked this conversation as resolved.
Show resolved Hide resolved
else:
try:
handler_signature.bind("self", "event")
except TypeError:
warnings.warn(
(
"Dubious signature for observe-decorated method. "
"The decorated method should be callable with a "
"single positional argument in addition to 'self'. "
"Did you forget to add an 'event' parameter?"
),
UserWarning,
stacklevel=2,
)

try:
observe_inputs = handler._observe_inputs
except AttributeError:
Expand Down
57 changes: 57 additions & 0 deletions traits/tests/test_observe.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,63 @@
)


class TestObserveDecorator(unittest.TestCase):
""" General tests for the observe decorator. """

def test_warning_on_handler_with_bad_signature(self):
message_regex = "should be callable with a single positional argument"

with self.assertWarnsRegex(UserWarning, message_regex):
class A(HasTraits):
foo = Int()

@observe("foo")
def _do_something_when_foo_changes(self):
pass

with self.assertWarnsRegex(UserWarning, message_regex):
class B(HasTraits):
foo = Int()

@observe("foo")
def _do_something_when_foo_changes(self, **kwargs):
pass

def test_decorated_method_signatures(self):
# Test different handler signatures for compatibility with
# observe decorator.

class A(HasTraits):
foo = Int()

call_count = Int(0)

@observe("foo")
def _the_usual_signature(self, event):
self.call_count += 1

@observe("foo")
def _method_with_extra_optional_args(self, event, frombicate=True):
self.call_count += 1

@observe("foo")
def _method_with_star_args(self, *args):
self.call_count += 1

@observe("foo")
def _method_with_alternative_name(self, foo_change_event):
self.call_count += 1

@observe("foo")
def _optional_second_argument(self, event=None):
self.call_count += 1

a = A()
self.assertEqual(a.call_count, 0)
a.foo = 23
self.assertEqual(a.call_count, 5)


class Student(HasTraits):
""" Model for testing list + post_init (enthought/traits#275) """

Expand Down