Skip to content

Commit f58b88a

Browse files
committed
Fixing __init__ for classes with @attr.s(slots=True).
1 parent 1affabe commit f58b88a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

mypyc/lib-rt/misc_ops.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
381381
if (!res) {
382382
goto fail;
383383
}
384+
// These attributes are added or modified by @attr.s(slots=True).
385+
const char * const keys[] = {"__attrs_attrs__", "__attrs_own_setattr__", "__init__", ""};
386+
for (const char * const *key_iter = keys; **key_iter != '\0'; key_iter++) {
387+
PyObject *value = PyObject_GetAttrString(res, *key_iter);
388+
if (value) {
389+
PyObject_SetAttrString(tp, *key_iter, value);
390+
Py_DECREF(value);
391+
}
392+
}
384393
Py_DECREF(res);
385394

386395
/* Copy back the original contents of the dict */

mypyc/test-data/run-classes.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,3 +2705,17 @@ print(native.ColorCode.OKGREEN.value)
27052705

27062706
[out]
27072707
okgreen
2708+
2709+
[case testAttrWithSlots]
2710+
import attr
2711+
2712+
@attr.s(slots=True)
2713+
class A:
2714+
ints: list[int] = attr.ib()
2715+
2716+
[file driver.py]
2717+
import native
2718+
print(native.A(ints=[1, -17]).ints)
2719+
2720+
[out]
2721+
\[1, -17]

0 commit comments

Comments
 (0)