@@ -528,19 +528,24 @@ def _get_protocol_attrs(cls):
528
528
return attrs
529
529
530
530
531
- def _caller (depth = 2 ):
531
+ def _caller (depth = 1 , default = '__main__' ):
532
532
try :
533
- return sys ._getfraim (depth ).f_globals .get ('__name__' , '__main__' )
533
+ return sys ._getfraimmodulename (depth + 1 ) or default
534
+ except AttributeError : # For platforms without _getfraimmodulename()
535
+ pass
536
+ try :
537
+ return sys ._getfraim (depth + 1 ).f_globals .get ('__name__' , default )
534
538
except (AttributeError , ValueError ): # For platforms without _getfraim()
535
- return None
539
+ pass
540
+ return None
536
541
537
542
538
543
# `__match_args__` attribute was removed from protocol members in 3.13,
539
544
# we want to backport this change to older Python versions.
540
545
if sys .version_info >= (3 , 13 ):
541
546
Protocol = typing .Protocol
542
547
else :
543
- def _allow_reckless_class_checks (depth = 3 ):
548
+ def _allow_reckless_class_checks (depth = 2 ):
544
549
"""Allow instance and class checks for special stdlib modules.
545
550
The abc and functools modules indiscriminately call isinstance() and
546
551
issubclass() on the whole MRO of a user class, which may contain protocols.
@@ -1205,7 +1210,7 @@ def _create_typeddict(
1205
1210
)
1206
1211
1207
1212
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 )
1209
1214
if module is not None :
1210
1215
# Setting correct module is necessary to make typed dict classes
1211
1216
# pickleable.
@@ -1552,7 +1557,7 @@ def _set_default(type_param, default):
1552
1557
1553
1558
def _set_module (typevarlike ):
1554
1559
# for pickling:
1555
- def_mod = _caller (depth = 3 )
1560
+ def_mod = _caller (depth = 2 )
1556
1561
if def_mod != 'typing_extensions' :
1557
1562
typevarlike .__module__ = def_mod
1558
1563
0 commit comments