-
-
Notifications
You must be signed in to change notification settings - Fork 3k
semanal: Check that async for/with is inside an async function #12418
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
Conversation
We should probably have an error code. Also, the format string for the message should be in the message registry. |
This comment has been minimized.
This comment has been minimized.
This can probably just go into the |
Is there a reason the |
Some SyntaxErrors are raised after parsing in the Python compiler, e.g. invalid nonlocal declarations, some things with async. So mypy has to replicate such checks in semanal. |
I have kept the error code as |
This comment has been minimized.
This comment has been minimized.
Yeah, syntax seems better if it already exists. |
Used |
mypy/semanal.py
Outdated
@@ -4173,6 +4183,11 @@ def visit_dictionary_comprehension(self, expr: DictionaryComprehension) -> None: | |||
self.analyze_comp_for_2(expr) | |||
|
|||
def visit_generator_expr(self, expr: GeneratorExpr) -> None: | |||
if True in expr.is_async: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An async genexp is actually legal in a non-async function: def f(): return (x async for x in y)
. Could you add a test for that?
Not sure we should use a blocking error. There's a risk that this check has a bug or that a future Python version changes the rules, and in that case it would be useful for users to be able to skip this error. I don't think we should be using blocking errors for anything we can recover from. |
This comment has been minimized.
This comment has been minimized.
As suggested, I've switched it back to non-blocking error, removed the error for generator expression, and added the remaining expressions into the test. |
This comment has been minimized.
This comment has been minimized.
It looks good. Can we change |
Done. |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Description
Added a check that async for/with is in an async function.
The code and error message are similar to those checking await.
Fixes #10594
Test Plan
I used an expanded example from the issue as a unit test. The example was extended to also cover for loop, with expression and usage outside of a function.
Also fixed another test case using async with expression outside of an async function.