Skip to content

Infer yield-from item type for concrete Iterable subclasses#21724

Open
apoorva-01 wants to merge 1 commit into
python:masterfrom
apoorva-01:fix-17449-yield-from-iterator
Open

Infer yield-from item type for concrete Iterable subclasses#21724
apoorva-01 wants to merge 1 commit into
python:masterfrom
apoorva-01:fix-17449-yield-from-iterator

Conversation

@apoorva-01

Copy link
Copy Markdown

Fixes #17449

yield from over an Iterator/Iterable subclass was inferring Any, so this slipped by:

def f() -> Iterator[str]:
    yield from Ints()  # Ints is Iterator[int], silently ok

get_generator_yield_type only handles the generator types themselves, and a concrete subclass isn't one, so it bailed to Any. Now it maps to Iterable and takes the item type, like a for loop.

Also dodges the args[0] trap #20983 hit (enumerate came out str instead of tuple[int, str]). tests cover both, both fail without the fix.

get_generator_yield_type fell back to Any for a yield from over a user-defined
Iterator subclass, hiding type mismatches; map it to Iterable like a for loop.

Fixes python#17449.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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