python / python/mypy

`has no attribute "__aiter__"; maybe "__enter__"? (not async iterable)` when async iterating

Open
#17,126 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

On mypy 1.9.0, this code suggests, somewhat nonsensically, __enter__ because __aiter__ does not exist despite the reference to __aiter__ being entirely implicit in the async for loop:

from typing import Self

class Foo:
    def __enter__(self) -> Self:
        return self

async def f() -> None:
    async for x in Foo():
        print(x)

On playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=3971471bd809ed97a72a1423a56f5318

Expected Behavior

Since the user did not type __aiter__, it is unlikely that it would be a typo for __enter__; therefore, probably the most helpful course would be to not give that suggestion.

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 reproducing the diagnostic with the Python example in the issue and compare it with the linked mypy playground result. Trace the async-for error and suggestion handling, then verify that an implicit aiter reference no longer suggests enter; the corrected diagnostic should still explain that Foo is not asynchronously iterable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.