@@ -2276,10 +2276,22 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
22762276
22772277 def parse_name (node ):
22782278 assert isinstance (node , ast .arg )
2279- if node .annotation is not None :
2280- raise ValueError ("Annotations are not currently supported" )
22812279 return node .arg
22822280
2281+ def parse_annotation (node ):
2282+ assert isinstance (node , (ast .arg , ast .FunctionDef ))
2283+ if isinstance (node , ast .arg ):
2284+ annotation = node .annotation
2285+ else :
2286+ annotation = node .returns
2287+ if annotation :
2288+ expr = ast .unparse (annotation )
2289+ try :
2290+ return eval (expr , sys_module_dict )
2291+ except NameError :
2292+ raise ValueError
2293+ return empty
2294+
22832295 def wrap_value (s ):
22842296 try :
22852297 value = eval (s , module_dict )
@@ -2334,7 +2346,11 @@ def p(name_node, default_node, default=empty):
23342346 default = ast .literal_eval (default_node )
23352347 except ValueError :
23362348 raise ValueError ("{!r} builtin has invalid signature" .format (obj )) from None
2337- parameters .append (Parameter (name , kind , default = default , annotation = empty ))
2349+ try :
2350+ annotation = parse_annotation (name_node )
2351+ except ValueError :
2352+ raise ValueError ("{!r} builtin has invalid signature" .format (obj )) from None
2353+ parameters .append (Parameter (name , kind , default = default , annotation = annotation ))
23382354
23392355 # non-keyword-only parameters
23402356 total_non_kw_args = len (f .args .posonlyargs ) + len (f .args .args )
@@ -2381,7 +2397,12 @@ def p(name_node, default_node, default=empty):
23812397 p = parameters [0 ].replace (kind = Parameter .POSITIONAL_ONLY )
23822398 parameters [0 ] = p
23832399
2384- return cls (parameters , return_annotation = cls .empty )
2400+ try :
2401+ return_annotation = parse_annotation (f )
2402+ except ValueError :
2403+ raise ValueError ("{!r} builtin has invalid signature" .format (obj )) from None
2404+
2405+ return cls (parameters , return_annotation = return_annotation )
23852406
23862407
23872408def _signature_from_builtin (cls , func , skip_bound_arg = True ):
0 commit comments