@@ -241,15 +241,8 @@ def __forward_code__(self):
241241 if self .__code__ is not None :
242242 return self .__code__
243243 arg = self .__forward_arg__
244- # If we do `def f(*args: *Ts)`, then we'll have `arg = '*Ts'`.
245- # Unfortunately, this isn't a valid expression on its own, so we
246- # do the unpacking manually.
247- if arg .startswith ("*" ):
248- arg_to_compile = f"({ arg } ,)[0]" # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
249- else :
250- arg_to_compile = arg
251244 try :
252- self .__code__ = compile (arg_to_compile , "<string>" , "eval" )
245+ self .__code__ = compile (_rewrite_star_unpack ( arg ) , "<string>" , "eval" )
253246 except SyntaxError :
254247 raise SyntaxError (f"Forward reference must be an expression -- got { arg !r} " )
255248 return self .__code__
@@ -987,7 +980,8 @@ def get_annotations(
987980 locals = {param .__name__ : param for param in type_params } | locals
988981
989982 return_value = {
990- key : value if not isinstance (value , str ) else eval (value , globals , locals )
983+ key : value if not isinstance (value , str )
984+ else eval (_rewrite_star_unpack (value ), globals , locals )
991985 for key , value in ann .items ()
992986 }
993987 return return_value
@@ -1024,6 +1018,16 @@ def annotations_to_string(annotations):
10241018 }
10251019
10261020
1021+ def _rewrite_star_unpack (arg ):
1022+ """If the given argument annotation expression is a star unpack e.g. `'*Ts'`
1023+ rewrite it to a valid expression.
1024+ """
1025+ if arg .startswith ("*" ):
1026+ return f"({ arg } ,)[0]" # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
1027+ else :
1028+ return arg
1029+
1030+
10271031def _get_and_call_annotate (obj , format ):
10281032 """Get the __annotate__ function and call it.
10291033
0 commit comments