python / python/mypy

await-not-async missed & false alarms with genexp and list comprehensions

Open
#18,124 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug good-first-issue topic-async topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

await-not-async does not warn on async for in list and dict comprehensions, and incorrectly does give a warning for await inside GeneratorExp.elt.
For list & dict comprehensions you should error on any await or async for, but for generator expressions it's only the generator that should be checked.

To Reproduce

def foo():
    ...

def should_not_error():
    (await x for x in foo())

def should_error():
    [x async for x in foo()]
    {k: v async for k, v in foo()}

def correctly_errors():
    (x for x in await foo())
    [await x for x in foo()]

def correctly_does_not_error():
    (x async for x in foo())

Expected Behavior

It should error on 8, 9, 12 and 13

Actual Behavior

$ mypy foofoo.py
foofoo.py:5: error: "await" outside coroutine ("async def")  [await-not-async]
foofoo.py:12: error: "await" outside coroutine ("async def")  [await-not-async]
foofoo.py:13: error: "await" outside coroutine ("async def")  [await-not-async]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

$ mypy --version
mypy 1.13.0 (compiled: yes)
  • Mypy version used:
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used:

also see https://github.com/astral-sh/ruff/issues/14167 that had the exact same issues

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the await-not-async implementation and the handling of GeneratorExp.elt, list comprehensions, and dict comprehensions. Reproduce the supplied example with mypy, then verify that diagnostics occur on lines 8, 9, 12, and 13 while the generator-expression cases on lines 5 and 14 do not warn.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.