Skip to content

False positive: "Incompatible return value type" #10059

@jdufresne

Description

@jdufresne
$ mypy --version
mypy 0.800

Input file:

from typing import Union

def f(v: Union[int, str]) -> str:
    if isinstance(v, str):
        try:
            v = int(v)
        except ValueError:
            # return as is
            return v
    return str(v)

Output:

$ mypy --strict ~/test.py
/home/jon/test.py:9: error: Incompatible return value type (got "Union[int, str]", expected "str")
Found 1 error in 1 file (checked 1 source file)

Expected: No error. All return statments can only return str.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions