Skip to content

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

Description

@DetachHead
fromtypingimportTypeVarT1=TypeVar("T1")
T2=TypeVar("T2")
deffoo(a: T1, b: T2) ->T1|T2:
returnainvalid=1if1elsefoo(1, "s") # error: Argument 2 to "foo" has incompatible type "str"; expected "int"valid=foo(1, "s") if1else1

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