Skip to content

Fix incorrect type inference for yield from with iterators#20983

Open
dhanusharer wants to merge 2 commits intopython:masterfrom
dhanusharer:fix-yield-from-inference
Open

Fix incorrect type inference for yield from with iterators#20983
dhanusharer wants to merge 2 commits intopython:masterfrom
dhanusharer:fix-yield-from-inference

Conversation

@dhanusharer
Copy link

Fixes #17449

Previously, when yield from was used with an Iterator[T], mypy
failed to correctly propagate the yielded item type and inferred Any.
This allowed incompatible generator return types to pass type checking.

This change extracts the iterator element type directly in
visit_yield_from_expr, enabling proper type comparison.

A regression test has been added.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

bandersnatch (https://github.com/pypa/bandersnatch)
+ src/bandersnatch_storage_plugins/filesystem.py: note: In member "scandir" of class "FilesystemStorage":
+ src/bandersnatch_storage_plugins/filesystem.py:213: error: Incompatible types in "yield from" (actual type "str", expected type "DirEntry[Any]")  [misc]

mypy (https://github.com/python/mypy)
+ mypy/report.py:150: error: Incompatible types in "yield from" (actual type "str", expected type "tuple[int, str]")  [misc]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong type inference with "yield from" from an Iterator class

1 participant