Skip to content

Decorator on method in derived class #1441

Closed

Description

@drewhaven

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.

Activity

  1. drewhaven commented on Apr 25, 2016

    @drewhaven
    Author

    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"

  2. gvanrossum commented on Apr 26, 2016

    @gvanrossum
    Member

    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().

  3. added this to the milestone on Apr 28, 2016
  4. removed this from the milestone on Mar 29, 2017
  5. self-assigned this
    on Jul 14, 2017
  6. gvanrossum commented on Aug 1, 2017

    @gvanrossum
    Member

    We really should fix this one!

  7. JukkaL commented on Aug 8, 2017

    @JukkaL
    Collaborator

    More examples in #2469.

  8. added a commit that references this issue on Sep 5, 2017
    3c4f624
  9. changed the title [-]Decorator on function in derived class[/-] [+]Decorator on method in derived class[/+] on Sep 8, 2017
  10. added a commit that references this issue on Sep 13, 2017
    a771113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions