python / python/mypy

Flow typing doesn't infer generic arguments

Aperta
#17,332 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 think the following should type check, but mypy errors on the last line. It's unable to infer that matching on the type of x.inner means we know that we have a Foo[str] and not a Foo[int].

#!/usr/bin/env python

from typing import *

T = TypeVar('T')

class Foo(Generic[T]):
    def __init__(self, inner: T) -> None:
        self.inner = inner

def foo(x: Foo[int] | Foo[str]) -> Foo[str]:
    match x.inner:
        case int():
            raise Exception("Unsupported")
        case str():
            return x # error: Incompatible return value type (got "Foo[int] | Foo[str]", expected "Foo[str]")  [return-value]

To Reproduce

Gist URL: https://gist.github.com/mypy-play/2be996e76f0d979d32be113f891803ed
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=2be996e76f0d979d32be113f891803ed

Expected Behavior

It should type check :)

Actual Behavior

error: Incompatible return value type (got "Foo[int] | Foo[str]", expected "Foo[str]")  [return-value]

Your Environment

  • Mypy version used: mypy 1.10.0 (compiled: yes)
  • Mypy command-line flags: `--strict``
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

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 il reproducer fornito per Python 3.12 con mypy 1.10.0 e --strict, usando il gist o il playground collegato. Traccia il comportamento del flow typing intorno al match su x.inner e verifica che l’esempio restituisca Foo[str] senza l’errore di ritorno incompatibile.

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
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.