Skip to content

mypy incorrectly identifies statements as unreachable when matching against constants #12545

@menma1234

Description

@menma1234

Bug Report

When using a match statement with cases that are constants, it incorrectly identifies the later cases as being unreachable.

To Reproduce

I have the following file structure:

project_root
    util
        __init__.py
        consts.py
        test.py

In consts.py:

BLANK = ""
SPECIAL = "asdf"

In test.py:

from util import consts


def test_func(test_str: str) -> str:
    match test_str:
        case consts.BLANK:
            return "blank"
        case consts.SPECIAL:
            return "special"
        case _:
            return "other"

Expected Behavior

mypy succeeds as the code is reachable.

Actual Behavior

Running mypy --warn-unreachable .\util\test.py gives:

util\test.py:9: error: Statement is unreachable
util\test.py:11: error: Statement is unreachable
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.942
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
  • Operating system and version: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions