python / python/mypy

ENH: Support slack variables (lower bound emulation)

Aperta
#20,648 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Describe the Bug

Context: https://github.com/python/typing/issues/674

Slack variables would allow us to emulate lower bounds on type variables, which is relevant for methods like dict.__or__ or list concatenation, when an outer context is present.

from typing import Never, assert_type, reveal_type

class Vec[T]:  # invariant in T
    def _items(self) -> list[T]: ...
    def _append(self, item: T) -> None: ...

def concat_vecs[T1, T2, _T_slack=Never](
    left: Vec[T1],
    right: Vec[T2],
) -> Vec[T1 | T2 | _T_slack]:  # <-- Slack variable makes return a supertype of T1 | T2
    return Vec()

class A: ...
class B: ...
class C: ...

def test_vec_concat(left: Vec[A], right: Vec[B]) -> None:
    # without outer context, the type is precise
    assert_type(concat_vecs(left, right), Vec[A | B])  # ✅️
    # outer context allows widening due to slack variables
    _0: Vec[A | B]     = concat_vecs(left, right) # ❌️
    _1: Vec[A | B | C] = concat_vecs(left, right) # ❌️
    _2: Vec[object]    = concat_vecs(left, right) # ❌️

mypy fails to find existing solutions on some of these function calls:

  • _0: (T1=A, T2=B, _T_slack=Never)
  • _1: (T1=A, T2=B, _T_slack=C)
  • _2: (T1=A, T2=B, _T_slack=object)
21:38: error: Argument 1 to "concat_vecs" has incompatible type "Vec[A]"; expected "Vec[A | B]"  [arg-type]
21:44: error: Argument 2 to "concat_vecs" has incompatible type "Vec[B]"; expected "Vec[A | B]"  [arg-type]
22:38: error: Argument 1 to "concat_vecs" has incompatible type "Vec[A]"; expected "Vec[A | B | C]"  [arg-type]
22:44: error: Argument 2 to "concat_vecs" has incompatible type "Vec[B]"; expected "Vec[A | B | C]"  [arg-type]
23:38: error: Argument 1 to "concat_vecs" has incompatible type "Vec[A]"; expected "Vec[object]"  [arg-type]
23:44: error: Argument 2 to "concat_vecs" has incompatible type "Vec[B]"; expected "Vec[object]"  [arg-type]

no playground link as typevar defaults are still not supported.

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 con la riproduzione di Vec e concat_vecs nell’issue, poi leggi l’issue 674 collegata di python/typing per il contesto della variabile slack. Non vengono indicati file del repository né test; il lavoro è completato quando le chiamate _0, _1 e _2 superano il controllo dei tipi con i tipi di risultato ampliati specificati.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.