Skip to content

bpo-40334: Add support for feature_version in new PEG parser #19827

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

Merged
merged 2 commits into from
May 1, 2020

Conversation

lysnikolaou
Copy link
Member

@lysnikolaou lysnikolaou commented May 1, 2020

ast.parse and compile support a feature_version parameter that
tells the parser to parse the input string, as if it were written in
an older Python version.
The feature_version is propagated to the tokenizer, which uses it
to handle the three different stages of support for async and
await. Additionally, it disallows the following at parser level:

  • The '@' operator in < 3.5
  • Async functions in < 3.5
  • Async comprehensions in < 3.6
  • Underscores in numeric literals in < 3.6
  • Await expression in < 3.5
  • Variable annotations in < 3.6
  • Async for-loops in < 3.5
  • Async with-statements in < 3.5
  • F-strings in < 3.6

Closes we-like-parsers#124.

https://bugs.python.org/issue40334

`ast.parse` and `compile` support a `feature_version` parameter that
tells the parser to parse the input string, as if it were written in
an older Python version.
The `feature_version` is propagated to the tokenizer, which uses it
to handle the three different stages of support for `async` and
`await`. Additionally, it disallows the following at parser level:
- The '@' operator in < 3.5
- Async functions in < 3.5
- Async comprehensions in < 3.6
- Underscores in numeric literals in < 3.6
- Await expression in < 3.5
- Variable annotations in < 3.6
- Async for-loops in < 3.5
- Async with-statements in < 3.5
- F-strings in < 3.6

Closes we-like-parsers#124.
@lysnikolaou lysnikolaou marked this pull request as ready for review May 1, 2020 01:21
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks pretty straightforward.

Comment on lines +163 to +166
| 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
_Py_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
| ASYNC 'for' t=star_targets 'in' ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
CHECK_VERSION(5, "Async for loops are", _Py_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. After all my efforts to combine the two rules... :-) (Seriously, it's probably clearer this way anyways.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, too.

@gvanrossum gvanrossum merged commit 3e0a6f3 into python:master May 1, 2020
@lysnikolaou lysnikolaou deleted the feature-version branch May 1, 2020 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Supporting feature_version=...
4 participants