Skip to content

Incompatible types in assignment (expression has type "bool", variable has type "Literal[False]") with nested code #13829

@shaolo1

Description

@shaolo1

To Reproduce

class Bar:
    def __init__(self, baz: bool) -> None:
        self.baz: bool = baz


def busted(key: int, bar: Bar) -> None:
    b = bar
    if key > 1:
        if b.baz: # No error without this block
            return
        if key != 1:
            baz = b.baz
    if key > 0:
        if key < 5:
            baz = b.baz  # error: Incompatible types in assignment (expression has type "bool", variable has type "Literal[False]")

Expected Behavior
This silly example is the smallest I could trim my real code to. It is a lot of nested if that stores references to bool properties in various levels of nesting. At no point is anything else assigned to the local variable (baz in this example).

Actual Behavior
error: Incompatible types in assignment (expression has type "bool", variable has type "Literal[False]")

Your Environment
mypy --version
mypy 0.981 (compiled: yes)
python --version
Python 3.10.6
Mypy command-line flags: None

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