python / python/typing

spec/conformance: function with TypeVar default and default argument used in context.

Aperta
#2,213 3 commenti 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: typing spec
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

I want to suggest adding the following test to the conformance test suite:

class Getter[T]:
    def get[S=None](self, default: S = None) -> T | S: ...  # OK

def test(arg: Getter[str]) -> None:
    result: str = arg.get()  # type: ignore
  • Since the default argument value is assignable to the default type var type, def get is well-defined
  • arg.get() implicitly uses None of type None as the argument. Therefore, we have an implicit constraint S <: None when .get is called without explicit default.

Therefore, result: str = arg.get() should raise an assignment type error (str | None not assignalbe to str).

Currently, only pyright succeeds on this test, and type checker results diverge.

  1. pyright: no errors (true negative on def get, true positive on result) (pyright playground)
  2. mypy: false positive on def get, false negative on result.
  3. ty: false positive on def get, true positive on result. (playground)
  4. pyrefly: false positive on def get, false negative on result (playground)
  5. zuban: false postive on def get, false negative on result.

Related Issues

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

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 individuando la suite di test di conformità di typing e i test esistenti per i valori predefiniti di TypeVar e gli argomenti predefiniti. Aggiungi l'esempio Getter proposto con la definizione prevista e le diagnostiche di assegnazione previste, quindi confronta i risultati tra i type checker elencati in modo che l'aspettativa di conformità sia esplicita.

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

Valutazione

Stack tecnologico
python
Ambito
testing
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 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.