#9457·mypy

Attribute narrowing isn't aware of side effects

Author: rggjanCreated Sep 18, 2020Updated Sep 17, 2026
Labelsbugtopic-literal-typestopic-enumtopic-reachabilitytopic-type-narrowing

This code:

class Foo:
    def __init__(self) -> None:
        self.dirty = False


def test_source() -> None:
    foo = Foo()

    def bar() -> None:
        foo.dirty = True

    assert foo.dirty is False

    bar()

    assert foo.dirty is True
    print("unreachable?")

produces the warning: main.py:17: error: Statement is unreachable, when running mypy --warn-unreachable test.py on 0.780.

Running the same through mypy 0.770 works fine and doesn't produce a warning.

(Also, running the example with pytest -s test.py prints unreachable?, because obviously this line is not unreachable when running the code in python).