Skip to content

Commit 4735de5

Browse files
committed
gh-136047 Allow typing._allow_reckless_class_checks to check _py_abc
When `abc.py` fails to import `_abc` and instead imports `_py_abc.ABCMeta`, `_py_abc.ABCMeta.__module__` is set to `abc` to allow `typing._allow_reckless_class_checks` to work with it. Unfortunately, when `typing._caller` falls back to using `sys._getframe`, its `globals()` contains the real module name instead of the module name of the frame's function. This patch allows checking for `_py_abc` in that scenario.
1 parent fb9f933 commit 4735de5

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

Lib/typing.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,11 +1843,15 @@ def _no_init_or_replace_init(self, *args, **kwargs):
18431843
cls.__init__(self, *args, **kwargs)
18441844

18451845

1846+
_RECKLESS_CLASS_CHECK_ALLOWED = {'abc', 'functools', None}
1847+
_SYS_HAS_GETFRAMEMODULENAME = hasattr(sys, '_getframemodulename')
1848+
if not _SYS_HAS_GETFRAMEMODULENAME:
1849+
_RECKLESS_CLASS_CHECK_ALLOWED.add('_py_abc')
1850+
1851+
18461852
def _caller(depth=1, default='__main__'):
1847-
try:
1853+
if _SYS_HAS_GETFRAMEMODULENAME:
18481854
return sys._getframemodulename(depth + 1) or default
1849-
except AttributeError: # For platforms without _getframemodulename()
1850-
pass
18511855
try:
18521856
return sys._getframe(depth + 1).f_globals.get('__name__', default)
18531857
except (AttributeError, ValueError): # For platforms without _getframe()
@@ -1860,7 +1864,7 @@ def _allow_reckless_class_checks(depth=2):
18601864
The abc and functools modules indiscriminately call isinstance() and
18611865
issubclass() on the whole MRO of a user class, which may contain protocols.
18621866
"""
1863-
return _caller(depth) in {'abc', 'functools', None}
1867+
return _caller(depth) in _RECKLESS_CLASS_CHECK_ALLOWED
18641868

18651869

18661870
_PROTO_ALLOWLIST = {

0 commit comments

Comments
 (0)