Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
menma1234 opened this issue Apr 7, 2022 · 2 comments · Fixed by #12751
Closed
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code

Comments

@menma1234
Copy link

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
@menma1234 menma1234 added the bug mypy got something wrong label Apr 7, 2022
@AlexWaygood AlexWaygood added topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code labels Apr 7, 2022
@Kurt-von-Laven
Copy link

Kurt-von-Laven commented Apr 12, 2022

Not sure whether this is related or separate, but I experience the same symptom in the following scenario:

from typing import Any

def test_structural_pattern_matching(x: Any) -> Any:
    match x:
        case {"x": {"y": [value]}}:
            print("mypy believes this line is unreachable")

Happy to file a separate issue if desired. Intriguingly, if you remove the outer dictionary, mypy stops complaining. This bug particularly impacts the use of structural pattern matching in code that handles JSON, which obviously frequently contains complex nested data structures.

JukkaL pushed a commit that referenced this issue May 9, 2022
Changes behaviour of PatternChecker so that ValuePattern does not cover
the entire (non-literal) type since it can catch only a single value.

Fixes: #12545

Signed-off-by: Štěpán Horáček <xhorace2@fi.muni.cz>
@Kurt-von-Laven
Copy link

Evidently the issue I mentioned was a separate one, so I filed #12770.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-reachability Detecting unreachable code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants