Skip to content

closured value not narrowed for immediately invoked lambda, but incorrectly narrowed for non-immediately invoked lambda #10993

Closed
@DetachHead

Description

@DetachHead
f: int | str

if isinstance(f, int):
    # error: Unsupported operand types for + ("str" and "int")
    # even though there's no chance the value could be a string here since the lambda is immediately invoked
    (lambda: f+1)()

    # no error, even though the lambda could get called later when the type changes
    asdf = lambda: f + 1

https://mypy-play.net/?mypy=latest&python=3.10&gist=9853f815e80f43e878bc9e1ae2d29108

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