Skip to content

[mypyc] Support decorated methods that override native methods - #22121

Open
VaggelisD wants to merge 2 commits into
python:masterfrom
VaggelisD:mypyc-decorated-override
Open

VaggelisD wants to merge 2 commits into
python:masterfrom
VaggelisD:mypyc-decorated-override

Conversation

@VaggelisD

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#767.

Decorating a method that overrides a native method crashed with KeyError:

class Base:
    def method(self) -> int:
        return 0

class Derived(Base):
    @deco
    def method(self) -> int:
        return 1

The override now gets a native glue method that calls the decorated attribute through the Python API, as suggested in the issue. It's used for the vtable slot and for direct native calls, so calls through a Base reference also go through the decorator. The undecorated method is still exported in the type dict under its original name, since the decorators are applied to it at runtime.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR! I found two apparent regressions, please have have a look. First, a decorated dunder that also exists on object now recurses forever:

from typing import Any

def deco(f: Any) -> Any:
    return lambda self: f(self)

class A:
    @deco
    def __repr__(self) -> str:
        return "A"

print(repr(A()))

Second, super() into a decorated override also recurses forever, but might have a different root cause (on master this generated KeyError, so the this is arguably a regression):

from typing import Any

def deco(f: Any) -> Any:
    return lambda self: f(self)

class Base:
    def m(self) -> int:
        return 1

class Der(Base):
    @deco
    def m(self) -> int:
        return 2

class Sub(Der):
    def m(self) -> int:
        return super().m() + 1

print(Sub().m())

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.

Can't decorate an overridden method

2 participants