[Python-Dev] FunctionDef.returns - explicit 'None' return type hint

Guido van Rossum guido at python.org
Thu Jan 19 14:54:35 EST 2017


On Thu, Jan 19, 2017 at 10:59 AM, Valentin Iovene via Python-Dev <
python-dev at python.org> wrote:

> With a ast.FunctionDef ast.AST node, is it possible to make the
> difference between this function
>
>     def hello_world():
>         print('hello world')
>
> and this one
>
>     def hello_world() -> None:
>         print('hello world')
>
> ?
>
> In both cases, the FunctionDef node has its 'returns' (return type
> hint) attribute set to None.


>>> t = compile('def f(): pass', '', 'exec', ast.PyCF_ONLY_AST)
>>> print(t.body[0].returns)
None
>>> t = compile('def f() -> None: pass', '', 'exec', ast.PyCF_ONLY_AST)
>>> print(t.body[0].returns)
<_ast.NameConstant object at 0x10a900f28>
>>>  print(t.body[0].returns.value)
None
>>>

-- 
--Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170119/5654368a/attachment.html>


More information about the Python-Dev mailing list