Skip to content

Strict Optional causes Generator variable to need explicit type annotation #2195

Closed
@FuegoFro

Description

@FuegoFro

Turning on the --strict-optional flag seems to be causing MyPy to fail to infer the type of a variable that holds a Generator function.

Python file

from typing import Generator
def test() -> Generator[str, None, None]:
    yield "Hello"

strings = test()

MyPy output

$ mypy_strict --strict-optional test.py
test.py:5: error: Need type annotation for variable

It's not happy about the strings variable. When I used reveal_type on test and test() the outputs seemed reasonable, but it's failing here. Adding an explicit type annotation to strings or making the function return Iterator[str] both fix the problem.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions