@@ -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. 
540545if  sys .version_info  >=  (3 , 13 ):
541546    Protocol  =  typing .Protocol 
542547else :
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
15531558def  _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