Skip to content

generic incorrectly narrowed based on first parameter in the else branch of an if expression #11049

Closed
@DetachHead

Description

@DetachHead
from typing import TypeVar

T1 = TypeVar("T1")
T2 = TypeVar("T2")


def foo(a: T1, b: T2) -> T1 | T2:
    return a

invalid = 1 if 1 else foo(1, "s") # error: Argument 2 to "foo" has incompatible type "str"; expected "int"
valid = foo(1, "s") if 1 else 1

https://mypy-play.net/?mypy=latest&python=3.10&gist=791645f9a348afc114cf3769ed85c30a

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions