Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e89d789

Browse filesBrowse files
authored
Update _caller() implementation (#598)
1 parent 34bfd84 commit e89d789
Copy full SHA for e89d789

File tree

Expand file treeCollapse file tree

1 file changed

+11
-6
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+11
-6
lines changed

‎src/typing_extensions.py

Copy file name to clipboardExpand all lines: src/typing_extensions.py
+11-6Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,24 @@ def _get_protocol_attrs(cls):
528528
return attrs
529529

530530

531-
def _caller(depth=2):
531+
def _caller(depth=1, default='__main__'):
532532
try:
533-
return sys._getframe(depth).f_globals.get('__name__', '__main__')
533+
return sys._getframemodulename(depth + 1) or default
534+
except AttributeError: # For platforms without _getframemodulename()
535+
pass
536+
try:
537+
return sys._getframe(depth + 1).f_globals.get('__name__', default)
534538
except (AttributeError, ValueError): # For platforms without _getframe()
535-
return None
539+
pass
540+
return None
536541

537542

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

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

15531558
def _set_module(typevarlike):
15541559
# for pickling:
1555-
def_mod = _caller(depth=3)
1560+
def_mod = _caller(depth=2)
15561561
if def_mod != 'typing_extensions':
15571562
typevarlike.__module__ = def_mod
15581563

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.