Skip to content

type narrowing leads to "unreachable" code being actually reachable #12598

Description

@Akuli

Bug Report

To Reproduce

class Foo:
def foo(self) -> None:
print("Foo!!!")
class Bar:
def __init__(self) -> None:
self.foo: Foo | None = Foo()
def get_rid_of_foo(self) -> None:
self.foo = None
bar = Bar()
if bar.foo is not None:
bar.get_rid_of_foo()
assert bar.foo is None
print(1 + "lol")

Expected Behavior

An error for the obviously wrong print(1 + "lol"). It fails there at runtime.

Actual Behavior

no errors!

Your Environment

mypy 0.942 with --python-version 3.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions