python / python/mypy

Matching on boolean expressions does not narrow based on the content of the boolean expression

Open
#18,833 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-match-statement
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When doing pattern matching with types, the match statement might be more readable than multiple if statements

match (a is None, b is None):
    case (True, True):
        ...
    case (True, False):
        ...
    case (False, True):
        ...
    case (False, False):
        ...

instead of

if a is None and b is None:
    ...
if a is None and b is not None:
    ...
if a is not None and b is None:
    ...
if a is not None and b is not None:
    ...

However, mypy is not able to infer types from the match statements like it is from if statements.

To Reproduce

Infer types using a match statement:

def example_with_match(a: str | None, b: str | None) -> str:
    match (a is None, b is None):
        case (True, True):
            return ""
        case (True, False):
            return b
        case (False, True):
            return a
        case (False, False):
            return a + b

    raise RuntimeError  # unreachable

Gist

Expected Behavior

Like in the equivalent using if statements (there are no mypy errors)

def example_with_if(a: str | None, b: str | None) -> str:
    if a is None and b is None:
        return ""
    if a is None and b is not None:
        return b
    if a is not None and b is None:
        return a
    if a is not None and b is not None:
        return a + b

    raise RuntimeError  # unreachable

Gist

Actual Behavior

whatever.py:6: error: Incompatible return value type (got "str | None", expected "str")  [return-value]
whatever.py:8: error: Incompatible return value type (got "str | None", expected "str")  [return-value]
whatever.py:10: error: Unsupported operand types for + ("str" and "None")  [operator]
whatever.py:10: error: Unsupported left operand type for + ("None")  [operator]

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
check_untyped_defs = true
no_implicit_optional = true
show_error_codes = true
warn_redundant_casts = true
warn_unreachable = true
python_version = "3.12"
ignore_missing_imports = true
  • Python version used: 3.12.8

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy on the supplied Python match-statement reproducer and compare it with the equivalent if-statement example. The issue names no source file or test path, so locate the pattern-matching type-narrowing entry point and relevant tests. Done means all four branches narrow a and b to str where appropriate without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.