Skip to content

[ty] Improve diagnostics for async generator stubs - #28692

Merged
charliermarsh merged 5 commits into
mainfrom
charlie/async-generator-stub-guidance
Sep 18, 2026
Merged

charliermarsh merged 5 commits into
mainfrom
charlie/async-generator-stub-guidance

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Summary

An async def stub without yield describes a coroutine function, even when its return annotation is AsyncIterator or AsyncGenerator. For example:

stubs.pyi:

from collections.abc import AsyncIterator

async def values() -> AsyncIterator[int]: ...

main.py:

from stubs import values

async def consume() -> None:
    async for value in values():
        pass

Before, the diagnostic identifies the missing iteration method but gives no guidance on correcting the stub:

error[not-iterable]: Object of type `CoroutineType[Any, Any, AsyncIterator[int]]` is not async-iterable
info: It has no `__aiter__` method

After, we point to the declaration and suggest how to declare an async generator. We also suggest await, since a coroutine can intentionally return an async iterator:

error[not-iterable]: Object of type `CoroutineType[Any, Any, AsyncIterator[int]]` is not async-iterable
 --> main.py:4:24
  |
4 |     async for value in values():
  |                        ^^^^^^^^
  |
 ::: stubs.pyi:3:1
  |
3 | async def values() -> AsyncIterator[int]: ...
  | ---------------------------------------- Without `yield` in the function body this function returns a coroutine
info: It has no `__aiter__` method
help: `await` the coroutine before iterating over its result
help: To declare `values` as an async generator, use `def` rather than `async def` or add `yield` to the body

For declarations in dependencies, we suggest reporting an incorrect stub to the library maintainers. The stub guidance also appears for incompatible callable return types, including method overrides, and is limited to functions with stub-like bodies. The typing rules are unchanged.

Closes astral-sh/ty#2875.

@charliermarsh charliermarsh added the ty The ty type checker label Sep 18, 2026
@astral-sh-bot

astral-sh-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.87%. The percentage of expected errors that received a diagnostic held steady at 97.78%. The number of fully passing files held steady at 130/145.

@astral-sh-bot

astral-sh-bot Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
prefect 460.99MB 461.02MB +0.01% (24.53kB) ⏫
flake8 38.24MB 38.24MB - ✅
sphinx 166.48MB 166.48MB - ✅
trio 91.88MB 91.88MB - ✅

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
Specialization 4.98MB 4.98MB +0.07% (3.45kB) ⏫
Type<'db>::class_member_with_policy_inner_ 9.94MB 9.94MB +0.03% (2.57kB) ⏫
FunctionType 5.97MB 5.97MB +0.03% (1.59kB) ⏫
GenericAlias 1.89MB 1.89MB +0.08% (1.48kB) ⏫
MemberLookupKey 7.09MB 7.09MB +0.02% (1.42kB) ⏫
Type<'db>::apply_specialization_inner_::interned_arguments 3.93MB 3.93MB +0.03% (1.41kB) ⏫
member_lookup_with_policy_inner 9.16MB 9.16MB +0.01% (1.34kB) ⏫
GenericContext 2.17MB 2.17MB +0.06% (1.34kB) ⏫
infer_scope_types_impl 19.41MB 19.41MB +0.01% (1.09kB) ⏫
Type<'db>::apply_specialization_inner_ 2.81MB 2.82MB +0.04% (1.02kB) ⏫
Type<'db>::cached_materialization_::interned_arguments 1.17MB 1.17MB +0.08% (960.00B) ⏫
GenericAlias<'db>::try_mro_ 4.04MB 4.04MB +0.02% (928.00B) ⏫
TypeVarSetInner 1.05MB 1.05MB +0.08% (888.00B) ⏫
Type<'db>::cached_materialization_ 954.28kB 955.08kB +0.08% (816.00B) ⏫
TypeVarInference 1.40MB 1.40MB +0.05% (720.00B) ⏫
... 20 more

@astral-sh-bot

astral-sh-bot Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh
charliermarsh marked this pull request as ready for review September 18, 2026 14:55
@charliermarsh
charliermarsh requested a review from a team as a code owner September 18, 2026 14:55
@astral-sh-bot
astral-sh-bot Bot requested a review from dhruvmanila September 18, 2026 14:55
@AlexWaygood AlexWaygood added the diagnostics Diagnostic messages, annotations, fixes & rendering infra--not for PRs that just add new diagnostics label Sep 18, 2026
@charliermarsh
charliermarsh requested a review from a team as a code owner September 18, 2026 15:59
@charliermarsh
charliermarsh marked this pull request as draft September 18, 2026 18:12
@charliermarsh
charliermarsh marked this pull request as ready for review September 18, 2026 18:12

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, thank you! A few more suggestions

@charliermarsh
charliermarsh enabled auto-merge (squash) September 18, 2026 18:37
@charliermarsh
charliermarsh merged commit 5980698 into main Sep 18, 2026
71 checks passed
@charliermarsh
charliermarsh deleted the charlie/async-generator-stub-guidance branch September 18, 2026 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Diagnostic messages, annotations, fixes & rendering infra--not for PRs that just add new diagnostics ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better diagnostic to help users annotate async generator in stub

2 participants