Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python type_comments #1649

Closed
Tracked by #1655 ...
maximiliankaul opened this issue Aug 8, 2024 · 2 comments · Fixed by #1701
Closed
Tracked by #1655 ...

Python type_comments #1649

maximiliankaul opened this issue Aug 8, 2024 · 2 comments · Fixed by #1701
Labels

Comments

@maximiliankaul
Copy link
Contributor

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.
@oxisto
Copy link
Member

oxisto commented Aug 14, 2024

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.

@oxisto
Copy link
Member

oxisto commented Aug 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants