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

TypeError when calling super() in decorated method #1038

Open
kdebrab opened this issue Oct 6, 2022 · 2 comments
Open

TypeError when calling super() in decorated method #1038

kdebrab opened this issue Oct 6, 2022 · 2 comments
Labels

Comments

@kdebrab
Copy link

kdebrab commented Oct 6, 2022

Following code:

import attrs


def decorated(method):
    def wrapped(self, *args, **kwargs):
        return method(self, *args, **kwargs)

    return wrapped


@attrs.define()
class A:
    def f(self):
        pass


@attrs.define()
class B(A):
    @decorated
    def f(self):
        super().f()


B().f()

raises following error

TypeError: super(type, obj): obj must be an instance or subtype of type

If I use super(B, self), everything works just fine.

@hynek
Copy link
Member

hynek commented Oct 6, 2022

This problem is related to cell rewriting for slotted classes. Put simply we have to cheat a bit to make them work by rewriting method contents.

You get the same behavior with dataclasses:

@dataclasses.dataclass(slots=True)
@attrs.define
class C:
    @decorated
    def f(self):
        super().f()
TypeError: super(type, obj): obj must be an instance or subtype of type

Short fixes:

  • don't use super that way
  • disable slots (@attrs.define(slots=False))

Long-term fixes:

  • open a bug on CPython for the dataclasses case and we'll steal it from them
  • convince @Tinche to fix it for attrs right away ;)

I'm a bit dubious if it's actually possible to fix tho. :-/ Maybe, if functools.wraps is used and we follow the __wrapped__ attributes?

@kdebrab
Copy link
Author

kdebrab commented Oct 6, 2022

Thanks for the quick response! I'll use super(B, self) for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants