FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-101162: Forbid using `issubclass` with `GenericAlias` as the 1st arg by sobolevn · Pull Request #103369 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4091,6 +4091,22 @@ class C(Generic[T]): pass
with self.assertRaises(TypeError):
C[()]

def test_generic_subclass_checks(self):
Comment thread
serhiy-storchaka marked this conversation as resolved.
for typ in [list[int], List[int],
tuple[int, str], Tuple[int, str],
typing.Callable[..., None],
collections.abc.Callable[..., None]]:
with self.subTest(typ=typ):
self.assertRaises(TypeError, issubclass, typ, object)
self.assertRaises(TypeError, issubclass, typ, type)
self.assertRaises(TypeError, issubclass, typ, typ)
Comment thread
sobolevn marked this conversation as resolved.
self.assertRaises(TypeError, issubclass, object, typ)

# isinstance is fine:
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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. */
Comment thread
serhiy-storchaka marked this conversation as resolved.
return recursive_issubclass(derived, cls);
}

Expand Down
1 change: 1 addition & 0 deletions Objects/genericaliasobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL