Skip to content

Commit 6d3f535

Browse files
authored
Make isinstance and issubclass error for GenericAlias (#6)
1 parent bb9a11f commit 6d3f535

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Lib/test/test_genericalias.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ def test_equality(self):
161161
self.assertNotEqual(list, list[int])
162162
self.assertNotEqual(list[int], list)
163163

164+
def test_isinstance(self):
165+
self.assertTrue(isinstance([], list))
166+
with self.assertRaises(TypeError):
167+
isinstance([], list[str])
168+
169+
def test_issubclass(self):
170+
class L(list): ...
171+
self.assertTrue(issubclass(L, list))
172+
with self.assertRaises(TypeError):
173+
issubclass(L, list[str])
174+
164175

165176
if __name__ == "__main__":
166177
unittest.main()

Objects/descrobject.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,8 +2061,26 @@ ga_mro_entries(PyObject *self, PyObject *args)
20612061
return PyTuple_Pack(1, alias->origin);
20622062
}
20632063

2064+
static PyObject *
2065+
ga_instancecheck(PyObject *self, PyObject *Py_UNUSED(ignored))
2066+
{
2067+
return PyErr_Format(PyExc_TypeError,
2068+
"TypeError: Subscripted generics cannot be used with class and instance checks",
2069+
self);
2070+
}
2071+
2072+
static PyObject *
2073+
ga_subclasscheck(PyObject *self, PyObject *Py_UNUSED(ignored))
2074+
{
2075+
return PyErr_Format(PyExc_TypeError,
2076+
"TypeError: Subscripted generics cannot be used with class and instance checks",
2077+
self);
2078+
}
2079+
20642080
static PyMethodDef ga_methods[] = {
20652081
{"__mro_entries__", ga_mro_entries, METH_O},
2082+
{"__instancecheck__", ga_instancecheck, METH_O},
2083+
{"__subclasscheck__", ga_subclasscheck, METH_O},
20662084
{0}
20672085
};
20682086

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy