python / python/mypy

Lenient TypeVarTuple checking when number of dimensions is unknown

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

Nessuno ha ancora preso questa issue.

feature needs discussion priority-0-high topic-pep-646
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Currently this generates an error:

from typing import Any

class C[*Ts, S]:
    pass

a = C[int, str]()
a = C[*tuple[Any, ...], str]()  # Error

This is the output:

error: Incompatible types in assignment (expression has type "C[*tuple[Any, ...], str]", variable has type "C[int, str]")

However, this doesn't generate an error:

from typing import Any

class C[*Ts, S]:
    pass

a = C[int, str]()
a = C[*tuple[Any, ...]]()  # No error

I'd argue that the first example shouldn't generate an error either. One possible rule would be to allow this as long as some substitution of the *tuple[Any, ...] part would match the target type.

We could possibly also generalize the matching of unknown-length TypeVarTuple type arguments when the unknown-length part has a non-Any tuple item type. This could reduce apparent false positives. Example:

from typing import Any

class C[*Ts, S]:
    pass

a = C[int, int]()
a = C[*tuple[int, ...]]()  # No error?

This would only change the behavior of variadic generics. Variable-length tuples would continue to behave as they behave currently, i.e. tuple[int, ...] wouldn't be assignable to tuple[int, int]. The tuple behavior has been around for a very long time, so it doesn't make sense to change it, but variadic generics are a relatively new and untested feature.

The primary motivation is help with NumPy and libraries that provide multidimensional array-like types.

Proper subtype checks should continue to work as they work currently, since we can't safely simplify say C[int] | C[*tuple[Any, ...]].

cc @ilevkivskyi who I chatted about this recently

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 con mypy i due esempi di TypeVarTuple dell’issue e confronta le diagnosi attuali. Analizza i percorsi di compatibilità dei variadic generics e di verifica dei sottotipi, quindi aggiungi la copertura per i casi proposti di lunghezza sconosciuta con Any e non-Any, preservando il comportamento esistente delle tuple di lunghezza variabile.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
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.