python / python/mypy

Incorrect send type with `yield from`

Open
#14,834 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Generators do not respect the send type when using yield from.

To Reproduce

def foo() -> Generator[None, int, None]:
    x: int = yield

def bar() -> Generator[None, str, None]:
    yield from foo()

Expected Behavior

Should error on yield from foo().

Actual Behavior

Does not error and allows gen = bar(); gen.send("abc"), which results in x: int = "abc".

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 running the Python reproduction in the issue and trace how mypy checks generator send types through yield from. Add a regression test covering foo() and bar(), and confirm that mypy reports an error at yield from foo() rather than allowing a string to be sent to the integer-typed generator.

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
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.