I don't pretend to have understood everything in python ast, but FunctionType is something that bothered me.
mod = Module(stmt* body, type_ignore *type_ignores)
| Interactive(stmt* body)
| Expression(expr body)
| FunctionType(expr* argtypes, expr returns)
| Suite(stmt* body)
What can it be?
FunctionTypeis the top-level symbol returned for parsing# type: (args) -> return_typecomments for function types (as specified by PEP484)The only authoritative source for this I can find is the changelog for Python 3.8 where it was added: https://docs.python.org/3/whatsnew/3.8.html#ast
You can parse this type of thing with
compileorast.parsewith the'func_type'mode (where the mode would usually beexecfor statements andevalfor expressions).This is only used (by external type checking software) if you are trying to annotate function return types with the Python 2 syntax, like:
Normal type annotations can just be parsed in
'eval'mode, but function type annotations can't be, so a new mode and top-level symbol was added:https://github.com/python/cpython/blob/main/Grammar/python.gram#L91
Which can be used by type checkers to parse these types of comments:
Which outputs: