@@ -253,22 +253,28 @@ def create(cls, obj, body, evaldict, defaults=None,
253253 attribute __source__ is added to the result. The attributes attrs
254254 are added, if any.
255255 """
256- if isinstance (obj , str ): # "name(signature)"
256+ if isinstance (obj , str ): # "name(signature)" or "name(signature) -> ret"
257257 name , rest = obj .strip ().split ('(' , 1 )
258- signature = rest [:- 1 ] # strip a right parens
258+ # split the argument list from an optional return annotation;
259+ # the argument list ends at the last right parens
260+ signature , _ , return_annotation = rest .rpartition (')' )
259261 func = None
260262 else : # a function
261263 name = None
262264 signature = None
265+ return_annotation = ''
263266 func = obj
264267 self = cls (func , name , signature , defaults , doc , module )
268+ self .return_annotation = return_annotation # e.g. " -> int"
265269 ibody = '\n ' .join (' ' + line for line in body .splitlines ())
266270 caller = evaldict .get ('_call_' ) # when called from `decorate`
267271 if caller and iscoroutinefunction (caller ):
268- body = ('async def %(name)s(%(signature)s):\n ' + ibody )
272+ body = ('async def %(name)s(%(signature)s)'
273+ '%(return_annotation)s:\n ' + ibody )
269274 body = re .sub (r'\breturn\b' , 'return await' , body )
270275 else :
271- body = 'def %(name)s(%(signature)s):\n ' + ibody
276+ body = ('def %(name)s(%(signature)s)'
277+ '%(return_annotation)s:\n ' + ibody )
272278 return self .make (body , evaldict , addsource , ** attrs )
273279
274280
0 commit comments