python / python/mypy

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

Aperta
#18,833 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-match-statement
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo mypy sul riproduttore Python fornito per un’istruzione match e confrontalo con l’esempio equivalente che usa un’istruzione if. L’issue non indica alcun file sorgente né alcun percorso dei test, quindi individua il punto di ingresso del restringimento dei tipi per il pattern matching e i test pertinenti. Il lavoro è completato quando tutti e quattro i rami restringono a e b a str dove appropriato, senza errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.