Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update _caller() implementation #598

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

Merged
merged 1 commit into from
May 24, 2025
Merged
Changes from all commits
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
17 changes: 11 additions & 6 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,19 +528,24 @@ def _get_protocol_attrs(cls):
return attrs


def _caller(depth=2):
def _caller(depth=1, default='__main__'):
try:
return sys._getframe(depth).f_globals.get('__name__', '__main__')
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()
return None
pass
return None


# `__match_args__` attribute was removed from protocol members in 3.13,
# we want to backport this change to older Python versions.
if sys.version_info >= (3, 13):
Protocol = typing.Protocol
else:
def _allow_reckless_class_checks(depth=3):
def _allow_reckless_class_checks(depth=2):
"""Allow instance and class checks for special stdlib modules.
The abc and functools modules indiscriminately call isinstance() and
issubclass() on the whole MRO of a user class, which may contain protocols.
Expand Down Expand Up @@ -1205,7 +1210,7 @@ def _create_typeddict(
)

ns = {'__annotations__': dict(fields)}
module = _caller(depth=5 if typing_is_inline else 3)
module = _caller(depth=4 if typing_is_inline else 2)
if module is not None:
# Setting correct module is necessary to make typed dict classes
# pickleable.
Expand Down Expand Up @@ -1552,7 +1557,7 @@ def _set_default(type_param, default):

def _set_module(typevarlike):
# for pickling:
def_mod = _caller(depth=3)
def_mod = _caller(depth=2)
if def_mod != 'typing_extensions':
typevarlike.__module__ = def_mod

Expand Down
Loading