Skip to content

gh-136047: Allow typing._allow_reckless_class_checks to check _py_abc #136115

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

youknowone
Copy link
Contributor

@youknowone youknowone commented Jun 30, 2025

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.

…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.
Lib/typing.py Outdated
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants