Skip to content

report an error when iterating over a generator if its second type parameter is not None #5758

Todo

Description

@DetachHead

Describe the Bug

looks like no type checker catches this:

from collections.abc import Generator
from typing import reveal_type


def foo() -> Generator[None, str]:
    asdf = yield
    reveal_type(asdf)  # pyrefly: str, runtime: None


for _ in foo():  # should be an error
    ...

i think iterating over a Generator should only be allowed if its _SendT_contra is None

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.w1tk4ihFwGUjqMiAlg6oJStacQRN0_n5kMQA1xsNShbgNBcLSxzFKWlAP1ZCShNICoQbpQGS1VRQUFaABjo4qwMLv9K8ksxcYC6BpG5Y8ogHpQ2wpVYgPcUZ-aU5KQpJqQqJeZDYhVigp6cHDkr0iCxKLU4tAbpFHRjn6njDDwA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. KotlinIsland commented on Nov 25, 2026

    @KotlinIsland
    Contributor
    def f() -> Generator[None, int]:
        (yield) + 1
    
    x: Iterator[None] = f()  # unsafe
  2. self-assigned this
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions