Skip to content
Merged
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1843,11 +1843,15 @@ def _no_init_or_replace_init(self, *args, **kwargs):
cls.__init__(self, *args, **kwargs)


_RECKLESS_CLASS_CHECK_ALLOWED = {'abc', 'functools', None}
_SYS_HAS_GETFRAMEMODULENAME = hasattr(sys, '_getframemodulename')
if not _SYS_HAS_GETFRAMEMODULENAME:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should just unconditionally allow _py_abc for simplicity.

We should also add a comment explaining when _py_abc is relevant.

Last, I think it might be possible to write a unit test for this; the test suite has a way to "block" import of a module that could be used to force the Python implementation of abc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should just unconditionally allow _py_abc for simplicity.

We should also add a comment explaining when _py_abc is relevant.

That sounds better.

Last, I think it might be possible to write a unit test for this; the test suite has a way to "block" import of a module that could be used to force the Python implementation of abc.

I have no idea how to write a test about this. Because abc is being used from deep inside of python bootstrapping, I wasn't able to do it on runtime. To test, I removed Modules/_abc.* files from the source.

_RECKLESS_CLASS_CHECK_ALLOWED.add('_py_abc')


def _caller(depth=1, default='__main__'):
try:
if _SYS_HAS_GETFRAMEMODULENAME:
return sys._getframemodulename(depth + 1) or default
except AttributeError: # For platforms without _getframemodulename()
pass
try:
return sys._getframe(depth + 1).f_globals.get('__name__', default)
except (AttributeError, ValueError): # For platforms without _getframe()
Expand All @@ -1860,7 +1864,7 @@ def _allow_reckless_class_checks(depth=2):
The abc and functools modules indiscriminately call isinstance() and
issubclass() on the whole MRO of a user class, which may contain protocols.
"""
return _caller(depth) in {'abc', 'functools', None}
return _caller(depth) in _RECKLESS_CLASS_CHECK_ALLOWED


_PROTO_ALLOWLIST = {
Expand Down
Loading