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
A decorator that changes the signature of a function is rejected if the old signature isn't consistent with the superclass. E.g.:
def convert_to_bool(f):
# type: (Callable[..., int]) -> Callable[..., bool]
def wrapper(*n, **kw):
i = f(*n, **kw)
return i < 10
return wrapper
class Base(object):
def f(self):
# type: () -> bool
return True
class Derived(Base):
@convert_to_bool
def f(self):
# type: () -> int
return 20
The interesting point here is that the base class doesn't use the decorator while the child does. Our particular issue is in code that uses contextlib.contextmanager, but I think we can work around it by adding the decorator to the base class.
Actually, that work-around doesn't currently work. Adding the decorator to Base.f still gives the error on Derived.f: Signature of "f" incompatible with supertype "Base"
Looking at the code a bit, it looks like check_method_override(), which is supposed to check whether Derived.f is compatible with Base.f, is called with the undecorated function. I'm guessing the decorator is stripped off somewhere a little earlier -- in fact, it seems the cuprit here is visit_decorator() that just calls e.func.accept().
Activity
drewhaven commentedon Apr 25, 2016
Actually, that work-around doesn't currently work. Adding the decorator to
Base.fstill gives the error onDerived.f:Signature of "f" incompatible with supertype "Base"gvanrossum commentedon Apr 26, 2016
Looking at the code a bit, it looks like check_method_override(), which is supposed to check whether Derived.f is compatible with Base.f, is called with the undecorated function. I'm guessing the decorator is stripped off somewhere a little earlier -- in fact, it seems the cuprit here is visit_decorator() that just calls e.func.accept().
gvanrossum commentedon Aug 1, 2017
We really should fix this one!
JukkaL commentedon Aug 8, 2017
More examples in #2469.
Fix type checking decorated method overrides
[-]Decorator on function in derived class[/-][+]Decorator on method in derived class[/+]Fix type checking decorated method overrides (#3918)