gh-136047: Allow typing._allow_reckless_class_checks to check _py_abc#136115
gh-136047: Allow typing._allow_reckless_class_checks to check _py_abc#136115JelleZijlstra merged 4 commits intopython:mainfrom
_py_abc#136115Conversation
…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
|
|
||
| _RECKLESS_CLASS_CHECK_ALLOWED = {'abc', 'functools', None} | ||
| _SYS_HAS_GETFRAMEMODULENAME = hasattr(sys, '_getframemodulename') | ||
| if not _SYS_HAS_GETFRAMEMODULENAME: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I wonder if we should just unconditionally allow
_py_abcfor simplicity.We should also add a comment explaining when
_py_abcis 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.
…check `_py_abc`" This reverts commit 4735de5.
When
abc.pyfails to import_abcand instead imports_py_abc.ABCMeta,_py_abc.ABCMeta.__module__is set toabcto allowtyping._allow_reckless_class_checksto work with it.Unfortunately, when
typing._callerfalls back to usingsys._getframe, itsglobals()contains the real module name instead of the module name of the frame's function.This patch allows checking for
_py_abcin that scenario.