Home > Enterprise >  TypeError when calling super() in dataclass(slots=True) subclass
TypeError when calling super() in dataclass(slots=True) subclass

Time:08-08

I am trying to call a a superclass method from a dataclass with slots=True in Python 3.10.5.

from dataclasses import dataclass


@dataclass(slots=True)
class Base:
    def hi(self):
        print("Hi")


@dataclass(slots=True)
class Sub(Base):
    def hi(self):
        super().hi()


Sub().hi()

I get the following error.

Traceback (most recent call last):
  File "...", line 16, in <module>
    Sub().hi()
  File "...", line 13, in hi
    super().hi()
TypeError: super(type, obj): obj must be an instance or subtype of type

It works fine if I remove slots=True from Sub, or make it a non-dataclass with __slots__ manually. The error remains if I instead do these to Base.

Sub.__mro__ is (<class '__main__.Sub'>, <class '__main__.Base'>, <class 'object'>) and isinstance(Sub(), Base) is True.

CodePudding user response:

As seen here, the dataclass decorator creates a new class object, and so the __closure__ attached to hi() is different to the one attached to the decorated class, and therefore the super() call cannot work without arguments due to relying on the __closure__.

Therfore, you need to change super().hi() to super(Sub, self).hi().

  • Related