Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101162: Forbid using issubclass with GenericAlias as the 1st arg #103369

Merged
merged 4 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4091,6 +4091,20 @@ class C(Generic[T]): pass
with self.assertRaises(TypeError):
C[()]

def test_generic_subclass_checks(self):
for typ in [list[int], List[int], tuple[int, str], Tuple[int, str],
dict[str, int], Dict[str, int], set[str], typing.Set[str]]:
with self.subTest(typ=typ):
self.assertRaises(TypeError, issubclass, typ, object)
self.assertRaises(TypeError, issubclass, typ, type)
self.assertRaises(TypeError, issubclass, typ, typ)

# isinstance is fine:
self.assertTrue(isinstance(typ, object))
self.assertTrue(isinstance(typ, object))
# but, not when the right arg is also a generic:
self.assertRaises(TypeError, isinstance, typ, typ)

def test_init(self):
T = TypeVar('T')
S = TypeVar('S')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Forbid using :func:`builtins.issubclass` with :class:`types.GenericAlias` as
the first argument.
2 changes: 1 addition & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2812,7 +2812,7 @@ object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)
return -1;
}

/* Probably never reached anymore. */
/* Can be reached when infinite recursion happens. */
return recursive_issubclass(derived, cls);
}

Expand Down
1 change: 1 addition & 0 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ ga_vectorcall(PyObject *self, PyObject *const *args,

static const char* const attr_exceptions[] = {
"__class__",
"__bases__",
"__origin__",
"__args__",
"__unpacked__",
Expand Down