Skip to content

Incorrect errors for fstrings + generators #3385

Closed
@ns-cweber

Description

@ns-cweber

mypy version: 0.511
python version: 3.6.1

Given this valid program:

from typing import Iterator

def stringify(it: Iterator[int]) -> Iterator[str]:
    return (f"{x}" for x in it)

def stringify2(it: Iterator[int]) -> Iterator[str]:
    for x in it:
        yield f"{x}"

def stringify3(it: Iterator[int]) -> Iterator[str]:
    return (str(x) for x in it)

def stringify4(it: Iterator[int]) -> Iterator[str]:
    for x in it:
        yield str(x)

I get errors for the first two variations:

foo.py:4: error: Generator has incompatible item type "int"
foo.py:8: error: Incompatible types in yield (actual type "int", expected type "str")

It seems that mypy doesn't recognize that fstrings are produce a str. Interestingly, the yield error didn't show up in my old mypy version 0.501.

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