You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to report a bug using handler defined by decorated function in a class with filtered event.
The following code reproduces all possible situations to add an handler defined w/wo decoration in a class or not, w/wo engine (or args), using an event w/wo filter
engine=Engine(lambdae, b: b)
# decoratordefdecorated(fun):
@functools.wraps(fun)defwrapper(*args, **kwargs):
returnfun(*args, **kwargs)
returnwrapper# handler as a functiondeffoo():
print("foo")
# handler as a decorated function@decorateddefdecorated_foo():
print("decorated_foo")
# register handler as a function -- OKengine.add_event_handler(Events.EPOCH_STARTED, foo)
# register handler as a function with filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), foo)
# register handler as a decorated function -- OKengine.add_event_handler(Events.EPOCH_STARTED, decorated_foo)
# register handler as a decorated function with filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), decorated_foo)
# handler as a function with engine (here args)deffoo_args(args):
print("foo_args", args)
# handler as a decorated function with engine @decorateddefdecorated_foo_args(args):
print("decorated_foo_args", args)
# register handler as a function with engine -- OKengine.add_event_handler(Events.EPOCH_STARTED, foo_args)
# register handler as a function with engine and filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), foo_args)
# register handler as a decorated function with engine -- OKengine.add_event_handler(Events.EPOCH_STARTED, decorated_foo_args)
# register handler as a decorated function with engine and filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), decorated_foo_args)
classFoo:
# handler as a class function (ie method)deffoo(self):
print("foo")
# handler as a decorated method@decorateddefdecorated_foo(self):
print("decorated_foo")
# handler as a method with enginedeffoo_args(self, args):
print("foo_args", args)
# handler as a decorated method with engine@decorateddefdecorated_foo_args(self, args):
print("decorated_foo_args", args)
foo=Foo()
# register handler as a method -- OKengine.add_event_handler(Events.EPOCH_STARTED, foo.foo)
# register handler as a method with filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), foo.foo)
# register handler as a decorated method -- OKengine.add_event_handler(Events.EPOCH_STARTED, foo.decorated_foo)
# register handler as a decorated method with filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), foo.decorated_foo)
# register handler as a method with engine -- OKengine.add_event_handler(Events.EPOCH_STARTED, foo.foo_args)
# register handler as a method with engine and filter -- OKengine.add_event_handler(Events.EPOCH_STARTED(every=2), foo.foo_args)
# register handler as a decorated method with engine -- OKengine.add_event_handler(Events.EPOCH_STARTED, foo.decorated_foo_args)
# register handler as a decorated method with engine and filter -- FAILEDengine.add_event_handler(Events.EPOCH_STARTED(every=2), foo.decorated_foo_args)
engine.run([0])
The error is
Error adding <function Foo.decorated_foo_args at 0x1229b6af0> 'handler': takes parameters ['self', 'args'] but will be called with [](missing a required argument: 'self').
The failed case is decorated method with engine. So, I guess functools.wraps works perfectly and catch self and engine as arguments. But the signature checking search (using inspect.signature) fails because missing self...
🐛 Bug description
I would like to report a bug using handler defined by decorated function in a class with filtered event.
The following code reproduces all possible situations to add an handler defined w/wo decoration in a class or not, w/wo engine (or args), using an event w/wo filter
The error is
Why ?
First, a handler defined with a filtered event is wrapped with decoration. See https://github.com/sdesrozis/ignite/blob/93be57aa3f71ce601391d59096c3b430c4d9487b/ignite/engine/engine.py#L198. Note that
functools.wraps
is used to fit the signature of the related handler.The failed case is decorated method with engine. So, I guess
functools.wraps
works perfectly and catchself
andengine
as arguments. But the signature checking search (usinginspect.signature
) fails because missingself
...See signature checking
ignite/ignite/engine/utils.py
Line 5 in 0de7156
I think this is related to
follow_wrapped=True
argument ofinspect.signature
.Environment
conda
,pip
, source): HondaThe text was updated successfully, but these errors were encountered: