@@ -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__
@@ -1025,7 +1018,8 @@ def get_annotations(
10251018 locals = {param .__name__ : param for param in type_params } | locals
10261019
10271020 return_value = {
1028- key : value if not isinstance (value , str ) else eval (value , globals , locals )
1021+ key : value if not isinstance (value , str )
1022+ else eval (_rewrite_star_unpack (value ), globals , locals )
10291023 for key , value in ann .items ()
10301024 }
10311025 return return_value
@@ -1062,6 +1056,16 @@ def annotations_to_string(annotations):
10621056 }
10631057
10641058
1059+ def _rewrite_star_unpack (arg ):
1060+ """If the given argument annotation expression is a star unpack e.g. `'*Ts'`
1061+ rewrite it to a valid expression.
1062+ """
1063+ if arg .startswith ("*" ):
1064+ return f"({ arg } ,)[0]" # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
1065+ else :
1066+ return arg
1067+
1068+
10651069def _get_and_call_annotate (obj , format ):
10661070 """Get the __annotate__ function and call it.
10671071
0 commit comments