You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Traceback (most recent call last):
File "C:\Users\michael.foord\Code\one-touch-switch-service\example.py", line 18, in <module>
t = Thing(1,3)
^^^^^^^^^^
File "<string>", line 6, in __init__
File "C:\Users\michael.foord\Code\one-touch-switch-service\example.py", line 16, in __post_init__
super().__post_init__()
^^^^^^^
TypeError: super(type, obj): obj must be an instance or subtype of type
The fix is to use Python 2 style super(Class, self).__post_init__().
First of all, I was not able to reproduce it without @dataclass, so I assumed it is related to the internal machinery.
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:
Traceback (most recent call last):
File "/Users/sobolev/Desktop/cpython2/ex.py", line 32, in <module>
Thing2(1, 2)
File "/Users/sobolev/Desktop/cpython2/ex.py", line 19, in __init__
self.__post_init__()
File "/Users/sobolev/Desktop/cpython2/ex.py", line 24, in __post_init__
super().__post_init__()
^^^^^^^^^^^^^^^^^^^^^
TypeError: super(type, obj): obj must be an instance or subtype of type Thing__slots Thing
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.
Activity
sobolevn commentedon Oct 30, 2023
I think that I found why this happens :)
First of all, I was not able to reproduce it without
@dataclass, so I assumed it is related to the internal machinery.This is my debug patch:
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! :)
sobolevn commentedon Oct 31, 2023
Here are some experiments that I had:
It fails with the same error. However, if you
pip install attrsand uncomment# from attr import dataclassit will work and printDONE!So, it can be fixed (given that our
_add_slotsfunctions are similar): https://github.com/python-attrs/attrs/blob/52c1cd97d723cde6d75259a22d218f9305ecf8ee/src/attr/_make.py#L809-L914sobolevn commentedon Oct 31, 2023
Here's the fix: https://github.com/python-attrs/attrs/blob/52c1cd97d723cde6d75259a22d218f9305ecf8ee/src/attr/_make.py#L884-L912 :)
pythongh-111500: Fix `super()` without args calls for `dataclasses` w…
voidspace commentedon Nov 6, 2023
Nice work @sobolevn
JelleZijlstra commentedon Mar 12, 2024
Duplicate of #90562
pythongh-111500: Fix `super()` without args calls for `dataclasses` w…