Skip to content

Commit d93b4ac

Browse files
authored
gh-101162: Forbid using issubclass() with GenericAlias as the 1st arg (GH-103369)
1 parent 666b68e commit d93b4ac

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

Lib/test/test_typing.py

+16
Original file line numberDiff line numberDiff line change
@@ -4091,6 +4091,22 @@ class C(Generic[T]): pass
40914091
with self.assertRaises(TypeError):
40924092
C[()]
40934093

4094+
def test_generic_subclass_checks(self):
4095+
for typ in [list[int], List[int],
4096+
tuple[int, str], Tuple[int, str],
4097+
typing.Callable[..., None],
4098+
collections.abc.Callable[..., None]]:
4099+
with self.subTest(typ=typ):
4100+
self.assertRaises(TypeError, issubclass, typ, object)
4101+
self.assertRaises(TypeError, issubclass, typ, type)
4102+
self.assertRaises(TypeError, issubclass, typ, typ)
4103+
self.assertRaises(TypeError, issubclass, object, typ)
4104+
4105+
# isinstance is fine:
4106+
self.assertTrue(isinstance(typ, object))
4107+
# but, not when the right arg is also a generic:
4108+
self.assertRaises(TypeError, isinstance, typ, typ)
4109+
40944110
def test_init(self):
40954111
T = TypeVar('T')
40964112
S = TypeVar('S')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Forbid using :func:`builtins.issubclass` with :class:`types.GenericAlias` as
2+
the first argument.

Objects/abstract.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)
28122812
return -1;
28132813
}
28142814

2815-
/* Probably never reached anymore. */
2815+
/* Can be reached when infinite recursion happens. */
28162816
return recursive_issubclass(derived, cls);
28172817
}
28182818

Objects/genericaliasobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ ga_vectorcall(PyObject *self, PyObject *const *args,
626626

627627
static const char* const attr_exceptions[] = {
628628
"__class__",
629+
"__bases__",
629630
"__origin__",
630631
"__args__",
631632
"__unpacked__",

0 commit comments

Comments
 (0)