Skip to content

Commit 526a218

Browse files
authored
[mypyc] Fix subclassing from abstract generic classes under 3.7 (#8121)
We need to set `__orig_bases__` before calling the metaclass or it will get mad.
1 parent ce0703a commit 526a218

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypyc/lib-rt/CPy.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ static PyObject *CPyType_FromTemplate(PyTypeObject *template_,
147147
if (!ns)
148148
goto error;
149149

150+
if (bases != orig_bases) {
151+
if (PyDict_SetItemString(ns, "__orig_bases__", orig_bases) < 0)
152+
goto error;
153+
}
154+
150155
dummy_class = (PyTypeObject *)PyObject_CallFunctionObjArgs(
151156
(PyObject *)metaclass, name, bases, ns, NULL);
152157
Py_DECREF(ns);

mypyc/test-data/run-classes.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ assert a.foo() == 11
438438
assert foo() == 21
439439

440440
[case testGenericClass]
441-
from typing import TypeVar, Generic
441+
from typing import TypeVar, Generic, Sequence
442442
T = TypeVar('T')
443443
class C(Generic[T]):
444444
x: T
@@ -449,6 +449,12 @@ class C(Generic[T]):
449449
def set(self, y: T) -> None:
450450
self.x = y
451451

452+
# Test subclassing generic classes both with and without a generic param
453+
class A(Sequence[int]):
454+
pass
455+
class B(Sequence[T]):
456+
pass
457+
452458
def f(c: C[int]) -> int:
453459
y = c.get()
454460
d = C[int](2)

0 commit comments

Comments
 (0)