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

Error code for "async for" outside async function is too general #18795

Open
danielnelson opened this issue Mar 12, 2025 · 1 comment
Open

Error code for "async for" outside async function is too general #18795

danielnelson opened this issue Mar 12, 2025 · 1 comment
Labels
bug mypy got something wrong topic-async async, await, asyncio topic-error-reporting How we report errors

Comments

@danielnelson
Copy link

Bug Report

The error code for "async for" outside async function [syntax] is very general, so it is hard to disable without allowing other syntax errors.

This is important to me because I want to type check Jupyter notebooks that take advantage of the autoawait feature and are written in the percent file format. With the error code being syntax, I would have to allow many unrelated errors to disable this error using disable_error_code.

I think it would be good if it used the same error code as "await" outside function [top-level-await], or perhaps a new code entirely.

To Reproduce

Example percent encoded notebook:

# %%
async def foo():
    return 1


await foo()


# %%
async def values():
    yield 1
    yield 2


async for v in values():
    print(v)

https://mypy-play.net/?mypy=latest&python=3.12&gist=ddd45a5b59946d0b423e5494af96e286

Expected Behavior

main.py:6: error: "await" outside function  [top-level-await]
main.py:15: error: "async for" outside async function  [top-level-await]

Actual Behavior

main.py:6: error: "await" outside function  [top-level-await]
main.py:15: error: "async for" outside async function  [syntax]

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12.9
@danielnelson danielnelson added the bug mypy got something wrong label Mar 12, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented Mar 13, 2025

#18152 fixes this I think, though no one with authority has said whether switching error codes like that is good.

@sterliakov sterliakov added topic-error-reporting How we report errors topic-async async, await, asyncio labels Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-async async, await, asyncio topic-error-reporting How we report errors
Projects
None yet
Development

No branches or pull requests

3 participants