Skip to content

Commit

Permalink
[mypyc] Fix subclassing from abstract generic classes under 3.7 (#8121)
Browse files Browse the repository at this point in the history
We need to set `__orig_bases__` before calling the metaclass or it
will get mad.
  • Loading branch information
msullivan authored Dec 10, 2019
1 parent ce0703a commit 526a218
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ static PyObject *CPyType_FromTemplate(PyTypeObject *template_,
if (!ns)
goto error;

if (bases != orig_bases) {
if (PyDict_SetItemString(ns, "__orig_bases__", orig_bases) < 0)
goto error;
}

dummy_class = (PyTypeObject *)PyObject_CallFunctionObjArgs(
(PyObject *)metaclass, name, bases, ns, NULL);
Py_DECREF(ns);
Expand Down
8 changes: 7 additions & 1 deletion mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ assert a.foo() == 11
assert foo() == 21

[case testGenericClass]
from typing import TypeVar, Generic
from typing import TypeVar, Generic, Sequence
T = TypeVar('T')
class C(Generic[T]):
x: T
Expand All @@ -449,6 +449,12 @@ class C(Generic[T]):
def set(self, y: T) -> None:
self.x = y

# Test subclassing generic classes both with and without a generic param
class A(Sequence[int]):
pass
class B(Sequence[T]):
pass

def f(c: C[int]) -> int:
y = c.get()
d = C[int](2)
Expand Down

0 comments on commit 526a218

Please sign in to comment.