Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Add checks for some 3.5+ only syntax (async/await and the @ operator) #41

Merged

Conversation

Michael0x2a
Copy link
Contributor

Python 3.5 introduced three new forms of syntax:

  • Coroutines with async/await (PEP 492)
  • The '@' operator (PEP 465)
  • Additional unpacking generalizations (PEP 448)

This commit adds a version check for the first two, but not the last item.

This commit was originally motivated by python/mypy#2401. I decided to also add a check for the '@' operator because it looked like an easy fix, but didn't add a check for the unpacking generalizations because that seemed harder.

The checks for async for, async with, and await are probably unnecessary because they can only be used inside async def anyways, which will always be checked first, but I figured I might as well add them just in case. I can remove them if you want, though, since it probably does incur a slight performance hit.

Python 3.5 introduced three new forms of syntax:

- Coroutines with async/await (PEP 492)
- The '@' operator (PEP 465)
- Additional unpacking generalizations (PEP 448)

This commit adds a version check for the first two, but
not the last item.

This commit was originally motivated by
python/mypy#2401. I decided
to also add a check for '@' because it looked like an
easy fix, but didn't add a check for the unpacking
generalizations because that seemed harder.
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

Copy link
Collaborator

@ddfisher ddfisher left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, Michael! This looks great! I just had a couple of tiny nits (see comments).

I agree that properly catching the PEP 448 additional unpacking generalizations will be a bit tricky, so I'm fine with it not being part of this PR.

I think it's fine to have checks for the async things that can only appear inside async functions. My understanding is that the parsing of those will change in 3.7 (async is becoming a keyword, I think), and it will be good to have reminders to give appropriate error messages when they do. (Now that I think about it, this might be a significant problem backwards-compatibility-wise, but that's unrelated to this PR.)

if (c->c_feature_version < 5) {
ast_error(c, n,
"Await expressions are only supported in Python 3.5 and greater");
return 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: this should be return NULL;.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, I think I must have forgotten to change this after copy-pasting. In any case, fixed.


If feature_version=4, the parser will currently forbid the use of the
async/await keywords and the '@' operator, but will not forbid the use of
PEP 448 additional unpacking generalizations, which was added in Python 3.5.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I'd consider some minor wording tweaks here. I think "When" is more commonly used than "If", in this context. I think you can get rid of "currently" -- docs by default describe current behavior (and there aren't any concrete plans to change this). Also, "additional unpacking generalizations" is plural, so I think the final "was" should be a "were".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mkay, sounds good -- hopefully the tweaked version flows better.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is great. 👍

@ddfisher
Copy link
Collaborator

(That said, I think that catching the PEP 448 generalizations might not be too terrible -- it looks to me like it could be done in ast_for_call with another couple variables and some careful thinking. Regardless, not required for this PR.)

Changes made:

- Return 'NULL' instead of 0 for await check
- Reword doc comment in typed_ast.ast3.parse
@ddfisher
Copy link
Collaborator

Thanks!

@ddfisher ddfisher merged commit 3e20710 into python:master Jun 13, 2017
@Michael0x2a Michael0x2a deleted the add-checks-for-some-python3.5-only-syntax branch June 13, 2017 21:35
gvanrossum pushed a commit to gvanrossum/mypy that referenced this pull request Jun 14, 2017
This incorporates python/typed_ast#41:
'Add checks for some 3.5+ only syntax (async/await and the @ operator)'
JukkaL pushed a commit to python/mypy that referenced this pull request Jun 14, 2017
This incorporates python/typed_ast#41:
'Add checks for some 3.5+ only syntax (async/await and the @ operator)'
tbbharaj pushed a commit to tbbharaj/typed_ast that referenced this pull request Dec 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants