Fraunhofer-AISEC/cpg

Python `type_comments`

maximiliankaul opened this issue · 2 comments

Investigate and make it work.

ast.parse = parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
    Parse the source into an AST node.
    Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
    Pass type_comments=True to get back type comments where the syntax allows.

After some investigation, it seems that type comments are mainly used in backporting type functionality into Python 2. Python 3 uses type annotations and we actually already use them at least for function return types and argument types.

Example for a type comment (This is parsed as an Assign with type_comment:

a = 42 # type int

Example for a type annotation on variable (this is parsed as an AnnAssign with annotation:

a: int = 42

Example for type annotation on function (this is parsed as returns on the FunctionDef as well as annotation on the arg):

def bar(x: int) -> int: ...

We currently only support parsing the last one, but in order to get as much type information as possible we should probably support all.

Let's keep this issue specifically about type comments. I created an umbrella issues for everything in PEP-484