Skip to content

Commit

Permalink
Make isinstance and issubclass error for GenericAlias (python#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatyping authored Jan 31, 2020
1 parent bb9a11f commit 6d3f535
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ def test_equality(self):
self.assertNotEqual(list, list[int])
self.assertNotEqual(list[int], list)

def test_isinstance(self):
self.assertTrue(isinstance([], list))
with self.assertRaises(TypeError):
isinstance([], list[str])

def test_issubclass(self):
class L(list): ...
self.assertTrue(issubclass(L, list))
with self.assertRaises(TypeError):
issubclass(L, list[str])


if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2061,8 +2061,26 @@ ga_mro_entries(PyObject *self, PyObject *args)
return PyTuple_Pack(1, alias->origin);
}

static PyObject *
ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return PyErr_Format(PyExc_TypeError,
"TypeError: Subscripted generics cannot be used with class and instance checks",
self);
}

static PyObject *
ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return PyErr_Format(PyExc_TypeError,
"TypeError: Subscripted generics cannot be used with class and instance checks",
self);
}

static PyMethodDef ga_methods[] = {
{"__mro_entries__", ga_mro_entries, METH_O},
{"__instancecheck__", ga_instancecheck, METH_O},
{"__subclasscheck__", ga_subclasscheck, METH_O},
{0}
};

Expand Down

0 comments on commit 6d3f535

Please sign in to comment.