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

'await' in non-async function is a blocking error #15339

Closed
xiaxinmeng opened this issue Jun 1, 2023 · 9 comments
Closed

'await' in non-async function is a blocking error #15339

xiaxinmeng opened this issue Jun 1, 2023 · 9 comments
Labels
topic-async async, await, asyncio topic-error-reporting How we report errors

Comments

@xiaxinmeng
Copy link

await in a function (not a async def) causes an error "errors prevented further checking". I think the expected behavior here is to assign an error code in this case rather than to halt.

To Reproduce

Run the following file with "mypy example.py":

def bar():
    return await (coro())

Expected Behavior:

MyPy does not halt checking, and it uses an error-code for "await" outside function.

Actual Behavior

Desktop/example.py:2: error: "await" outside coroutine ("async def")
Found 1 error in 1 file (errors prevented further checking)

Environment

Mypy version: 1.3.0
Python version: 3.11.3
Operating System: Ubuntu 18.04
How to install MyPy: "python3.11 -m pip install mypy"

@JelleZijlstra JelleZijlstra changed the title Mypy halts if 'await' is in a function 'await' is in non-async function is a blocking error Jun 1, 2023
@JelleZijlstra JelleZijlstra added topic-error-reporting How we report errors topic-async async, await, asyncio labels Jun 1, 2023
@JelleZijlstra JelleZijlstra changed the title 'await' is in non-async function is a blocking error 'await' in non-async function is a blocking error Jun 1, 2023
@gregorysantosa
Copy link
Contributor

Hello, I'd like to begin working on this issue, would that be fine?

@gregorysantosa
Copy link
Contributor

I have created a PR that references the issue, however, I have not added any tests for the new error code, any guidance on how to proceed would be appreciated. Thank you!

@xiaxinmeng
Copy link
Author

Thank you. Several other keywords may also cause similar blocking issues, such as "yield" and "break." I think these should also be taken into consideration.

For example:

  1. break outside loop
with cm:
    A
    break
  1. yield from outside function
    yield 1

  2. continue outside loop

if 1:
    continue

@gregorysantosa
Copy link
Contributor

gregorysantosa commented Jun 6, 2023

Hello, when I try to implement an error code for each of these blocking issues I encounter an "IndexError: List out of range" error. I believe this is due to handle_continue attempting to iterate through a list when it has no contents however I am not certain if this is the case. This occurred for all 3 example scenarios described by @xiaxinmeng. I am not sure how to proceed.

@JelleZijlstra
Copy link
Member

I'm generally in support of reducing the number of blocking errors, but if it requires complex changes to how we deal with loops, it's probably not worth changing anything here. Such code is a SyntaxError at runtime anyway.

@gregorysantosa
Copy link
Contributor

That sounds good, and I have a question about the original scenario's "await" not in-async error. I created an error code AWAIT_NOT_ASYNC and assigned it to one of the cases in the visit_await_expr function located in semanal.py and I saw no errors with that code. However, I have not yet added test cases for the function I added. Any guidance on where to add test cases would be appreciated.

@hauntsaninja
Copy link
Collaborator

I think if you fix the test case that is failing on your PR that should be enough (testInvalidComprehensionNoCrash)

@gregorysantosa
Copy link
Contributor

gregorysantosa commented Jun 7, 2023

I have closed the old PR that referenced the error and added a new PR that includes:

  1. The new error code for await statements that aren't in async functions
  2. Modifications to visit_await_expr within semanal.py to print an error code instead of producing a blocking error
  3. The modified testInvalidComprehensionNoCrash case to accurately reflect updates to the code

I believe the changes in the PR should close this issue. Please let me know if any more work should be done on this issue.

@AlexWaygood
Copy link
Member

Fixed by #15384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-async async, await, asyncio topic-error-reporting How we report errors
Projects
None yet
Development

No branches or pull requests

5 participants