Skip to content

[1.20 regression] False positive for checking reachability on dict with single-variant enum keys #21409

@kmurphy4

Description

@kmurphy4

Bug Report
After upgrading from 1.19.x to 1.20.x, I'm seeing a false positive with reachability checking for dicts with single-variant Enum keys.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&flags=warn-unreachable&gist=3ba2a6f89ea69ebf0ce78f0d91fb0e7f

import enum

class Color(enum.Enum):
    RED = enum.auto()

def foo(colors: list[Color]) -> None:
    counts: dict[Color, int] = {}
    for color in colors:
        if color not in counts:
            counts[color] = 0  # error: Statement is unreachable  [unreachable]
        counts[color] += 1

If I add a second Enum variant, the error goes away.

Interestingly, the error also goes away if I remove the explicit type annotation.

Expected Behavior

With v1.19.1, I get no errors.

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