python / python/mypy

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

Offen
#18,795 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-async topic-error-reporting
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte percent-encoded Notebook-Beispiel mit mypy 1.15.0 und Python 3.12 zu reproduzieren. Verfolge, wie Diagnosen für top-level await und async for zugewiesen werden, und überprüfe anschließend, dass der async-for-Fehler erwartungsgemäß [top-level-await] meldet, ohne andere Syntaxdiagnosen zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.