Skip to content

Mandatory Iterator with "yield" #633

Closed
@fcolas

Description

@fcolas

Is it intended that the use of yield mandates the return type Iterator?
For example, if I want the squares of numbers:

def get_squares(n: int) -> Iterable[int]:
    return [i**2 for i in range(n)]

I cannot reimplement it as an iterator:

def get_squares(n: int) -> Iterable[int]:
    for i in range(n):
        yield i**2

without getting Iterator function return type expected for "yield" (even if Iterator actually is a subclass of Iterable).

However, it works with yield from:

def get_squares(n: int) -> Iterable[int]:
    yield from (i**2 for i in range(n))

What is the reasoning?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions