Skip to content

mypy is confused by return values from generators #695

Closed
@gvanrossum

Description

@gvanrossum

This is fine:

from typing import Iterator

def foo() -> Iterator[int]:
    yield 42

x = next(foo()) + 1000
print(x)

But now try this:

from typing import Iterator

def foo() -> Iterator[int]:
    yield 42
    return 'abc'

This complains about the return statement:

g.py, line 5: Incompatible return value type: expected typing.Iterator[builtins.int], got builtins.str

It's matching the generator return value (which is only accessible by a caller that uses yield from) to the declared return type (which is just the type of what you get by calling foo().

Metadata

Metadata

Assignees

Labels

bugmypy got something wrong

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions