-
-
Notifications
You must be signed in to change notification settings - Fork 206
Closed
Description
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:
- Undoing this fix manually after each run of
pyupgrade. - Remove the use of
slots=Truein@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
Labels
No labels