Skip to content

Breaks super call with @dataclasses.dataclass(slots=True) #1032

@nthykier

Description

@nthykier

Minimal reproducer:

#!/usr/bin/python3

from dataclasses import dataclass


@dataclass(slots=True)
class Parent:
    msg: str

    def do_something(self) -> None:
        print(self.msg)


@dataclass(slots=True)
class Child(Parent):
    custom_message: str | None

    def do_something(self) -> None:
        super(Child, self).do_something()
        if self.custom_message is None:
            print(self.custom_message)


if __name__ == "__main__":
    c = Child("A", None)
    c.do_something()

This works and prints A. When running pyupgrade on this, the transformed code fails with

Traceback (most recent call last):
  File "/path/to/code/dataclass.py", line 26, in <module>
    c.do_something()
    ~~~~~~~~~~~~~~^^
  File "/path/to/code/dataclass.py", line 19, in do_something
    super().do_something()
    ^^^^^^^^^^^^^^^^^^^^
TypeError: super(type, obj): obj (instance of Child) is not an instance or subtype of type (Child).

The delta is:

--- dataclass.py        2025-09-21 09:00:04.834027765 +0200
+++ dataclass.py        2025-09-21 09:00:15.382230860 +0200
@@ -17,7 +17,7 @@
 
     def do_something(self) -> None:
         if self.custom_message is None:
-            super(Child, self).do_something()
+            super().do_something()
         else:
             print(self.custom_message)

Workarounds that I am aware of include:

  1. Undoing this fix manually after each run of pyupgrade.
  2. Remove the use of slots=True in @dataclass.

I find neither compelling alternatives, so I am hoping this can be fixed.

Tested with 3.20 on Python3.13.7.

Related CPython bug (python/cpython#90562) is allegedly closed but it also has an open PR (python/cpython#124692). I cannot find any mentions of it being fixed in the "what changed" for Python 3.14 nor 3.15 (but both may be incomplete at the time of writing since they are not released).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions