-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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
dataclass, slots, __post_init__ and super #111500
Comments
I think that I found why this happens :) First of all, I was not able to reproduce it without This is my debug patch: » git patch
diff --git Lib/dataclasses.py Lib/dataclasses.py
index 2fba32b5ffb..a7ce3c03e9b 100644
--- Lib/dataclasses.py
+++ Lib/dataclasses.py
@@ -1232,7 +1232,7 @@ def _add_slots(cls, is_frozen, weakref_slot):
# And finally create the class.
qualname = getattr(cls, '__qualname__', None)
- cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
+ cls = type(cls)(cls.__name__ + '__slots', cls.__bases__, cls_dict)
if qualname is not None:
cls.__qualname__ = qualname
diff --git Objects/typeobject.c Objects/typeobject.c
index 25085693070..79db15579e5 100644
--- Objects/typeobject.c
+++ Objects/typeobject.c
@@ -10400,9 +10400,11 @@ supercheck(PyTypeObject *type, PyObject *obj)
Py_XDECREF(class_attr);
}
- PyErr_SetString(PyExc_TypeError,
+ PyErr_Format(PyExc_TypeError,
"super(type, obj): "
- "obj must be an instance or subtype of type");
+ "obj must be an instance or subtype of type %s %s",
+ Py_TYPE(obj)->tp_name,
+ type->tp_name);
return NULL;
}
Turns out, I was right. Given the code in the original bug report, it produces:
Looks like we add slots and regenerate class in the very end. I think we can try doing it at the beggining. Maybe it will help. I am working on this idea! :) |
Here are some experiments that I had: from dataclasses import dataclass
# from attr import dataclass
@dataclass(slots=True)
class Base:
def m(self):
return "DONE!"
class Thing(Base):
def m(self):
print(super().m())
Thing2 = dataclass(slots=True)(Thing)
Thing2().m() It fails with the same error. However, if you So, it can be fixed (given that our |
Nice work @sobolevn |
Duplicate of #90562 |
Bug report
Bug description:
Using
super()
in__post_init__
on a dataclass with slots fails.This code:
Produces this exception:
The fix is to use Python 2 style
super(Class, self).__post_init__()
.CPython versions tested on:
3.11
Operating systems tested on:
Windows
Linked PRs
super()
without args calls fordataclasses
with slots #111538The text was updated successfully, but these errors were encountered: