@@ -1024,15 +1024,8 @@ def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
10241024 if not isinstance (arg , str ):
10251025 raise TypeError (f"Forward reference must be a string -- got { arg !r} " )
10261026
1027- # If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
1028- # Unfortunately, this isn't a valid expression on its own, so we
1029- # do the unpacking manually.
1030- if arg .startswith ('*' ):
1031- arg_to_compile = f'({ arg } ,)[0]' # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
1032- else :
1033- arg_to_compile = arg
10341027 try :
1035- code = compile (arg_to_compile , '<string>' , 'eval' )
1028+ code = compile (_rewrite_star_unpack ( arg ) , '<string>' , 'eval' )
10361029 except SyntaxError :
10371030 raise SyntaxError (f"Forward reference must be an expression -- got { arg !r} " )
10381031
@@ -1119,6 +1112,16 @@ def __repr__(self):
11191112 return f'ForwardRef({ self .__forward_arg__ !r} { module_repr } )'
11201113
11211114
1115+ def _rewrite_star_unpack (arg ):
1116+ """If the given argument annotation expression is a star unpack e.g. `'*Ts'`
1117+ rewrite it to a valid expression.
1118+ """
1119+ if arg .startswith ("*" ):
1120+ return f"({ arg } ,)[0]" # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
1121+ else :
1122+ return arg
1123+
1124+
11221125def _is_unpacked_typevartuple (x : Any ) -> bool :
11231126 return ((not isinstance (x , type )) and
11241127 getattr (x , '__typing_is_unpacked_typevartuple__' , False ))
0 commit comments