python / python/mypy

Class structural pattern matching always returns `Any` as type

Aperta
#17,589 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

I just hit this issue and I am not sure if it's is a problem of "join-vs-union" or if it is an actual bug.

I am returning a return's ResultE from a function and then I am parsing the result using match. The type of the values matched in the class structural matching always resolve to Any, even though mypy recognizes the type of the return value to have a specific combination of types:

Revealed type is "returns.result.Result[builtins.bool, builtins.Exception]"

To Reproduce

In an environment with returns installed:

from typing import reveal_type

from returns.result import Failure, Success, safe


@safe
def safe_function(value: int) -> bool:
    if (value % 2) == 0:
        raise Exception("It's not odd!")

    return True


def pattern_matcher() -> bool:
    function_out = safe_function(12)
    reveal_type(function_out)
    match function_out:
        case Success(result):
            reveal_type(result)
            return result
        case Failure(err):
            reveal_type(err)
            print(err)

    return False

Expected Behavior

Mypy validates the file correctly with the following output:

$ mypy match_test.py
match_test.py:16: note: Revealed type is "returns.result.Result[builtins.bool, builtins.Exception]"
match_test.py:19: note: Revealed type is "builtins.bool"
match_test.py:22: note: Revealed type is "builtins.Exception"
Success: no issues found in 1 source file

Actual Behavior

Mypy returns error with the following output:

$ mypy match_test.py
match_test.py:16: note: Revealed type is "returns.result.Result[builtins.bool, builtins.Exception]"
match_test.py:19: note: Revealed type is "Any"
match_test.py:20: error: Returning Any from function declared to return "bool"  [no-any-return]
match_test.py:22: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

Extra test

I changed the match pattern to this: case Success(bool(result)): and mypy did not return any error:

from typing import reveal_type

from returns.result import Failure, Success, safe


@safe
def safe_function(value: int) -> bool:
    if (value % 2) == 0:
        raise Exception("It's not odd!")

    return True


def pattern_matcher() -> bool:
    function_out = safe_function(12)
    reveal_type(function_out)
    match function_out:
        case Success(bool(result)):
            reveal_type(result)
            return result
        case Failure(err):
            reveal_type(err)
            print(err)

    return False

Output:

$ mypy match_test.py
match_test.py:16: note: Revealed type is "returns.result.Result[builtins.bool, builtins.Exception]"
match_test.py:19: note: Revealed type is "builtins.bool"
match_test.py:22: note: Revealed type is "Any"
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: mypy 1.11.0 (compiled: yes)
  • Mypy configuration options from mypy.ini (and other config files):
    [tool.mypy]
    python_version = "3.12"
    mypy_path = "$MYPY_CONFIG_FILE_DIR/src"
    enable_incomplete_feature = ["NewGenericSyntax"]
    ignore_missing_imports = true
    no_implicit_optional = true
    strict_optional = true
    strict = true
    plugins = ["returns.contrib.mypy.returns_plugin"]
    disallow_untyped_defs = true
    
  • Python version used: Python 3.12.4
  • Returns version used: 0.23.0

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

Esegui il reproducer match_test.py fornito con mypy 1.11.0 e confronta i tipi rivelati per Success(result) con quelli per Success(bool(result)). Traccia l’inferenza dei tipi del pattern matching strutturale delle classi coinvolta in questi casi e aggiungi una copertura di regressione. Il lavoro è completato quando i tipi del payload generico vengono preservati, vengono rivelati bool ed Exception invece di Any e non vengono introdotti 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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.