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

Supporting type_comments=True #95

Closed
gvanrossum opened this issue Apr 19, 2020 · 3 comments · Fixed by python/cpython#19780
Closed

Supporting type_comments=True #95

gvanrossum opened this issue Apr 19, 2020 · 3 comments · Fixed by python/cpython#19780
Assignees
Labels
3.9.0b1 Needs to be fixed/implemented until the 3.9.0b1 release

Comments

@gvanrossum
Copy link

gvanrossum commented Apr 19, 2020

The ast.parse() function has two barely documented features, type_comments= and feature_version=, which modify the tokenizer and parser for the benefit of (primarily) mypy. type_comments=True changes the tokenizer to return TYPE_COMMENT tokens when it detects a type comment (a comment starting with 'type:'), and the old Grammar file has [TYPE_COMMENT] sprinkled throughout to support these.

In addition, feature_version=(3, N) enables a few changes in the tokenizer and ast.c to support older Python versions (mostly related to the three different stages of support for async and await keywords). [moved to #124]

We'll have to support these. I think it won't be very difficult since most of the logic is part of the tokenizer, but we'll have to check ast.c for other tests for feature_version. Note that these flags are passed in different forms to compile(), which has the actual support, but undocumented. See the source of ast.py.

@gvanrossum
Copy link
Author

IMO for alpha6 we can just make these flags use the old parser. But we do need to do that (else mypy won't be able to work with alpha 6 unless you use -X oldparser).

@lysnikolaou
Copy link

lysnikolaou commented Apr 21, 2020

IMO for alpha6 we can just make these flags use the old parser. But we do need to do that (else mypy won't be able to work with alpha 6 unless you use -X oldparser).

Done in 0bd4bc3, which needs review though.

@gvanrossum
Copy link
Author

gvanrossum commented Apr 26, 2020

Okay, I am looking into how to do this post-alpha6. (FWIW I've fixed mypy to work with the 3.9 but you need to use master from https://github.com/python/mypy/.)

There are actually several separate things to implement:

  • support for type comments in the grammar other than long-form arguments
  • long-form argument type comments (this looks very hairy)
  • support for # type: ignore
  • support for func_type_input

@gvanrossum gvanrossum self-assigned this Apr 26, 2020
@lysnikolaou lysnikolaou added the 3.9.0b1 Needs to be fixed/implemented until the 3.9.0b1 release label Apr 28, 2020
@gvanrossum gvanrossum changed the title Supporting type_comments= and feature_version= Supporting type_comments=True Apr 29, 2020
gvanrossum added a commit to python/cpython that referenced this issue Apr 30, 2020
This implements full support for # type: <type> comments, # type: ignore <stuff> comments, and the func_type parsing mode for ast.parse() and compile().

Closes we-like-parsers#95.

(For now, you need to use the master branch of mypy, since another issue unique to 3.9 had to be fixed there, and there's no mypy release yet.)

The only thing missing is `feature_version=N`, which is being tracked in we-like-parsers#124.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9.0b1 Needs to be fixed/implemented until the 3.9.0b1 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants