python / python/typeshed

type annotation for `{dict_keys,dict_items}.is_disjoint` argument is too narrow

Aperta
#15,888 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

In the PR that added these annotations, @hauntsaninja said:

My instinct is to try it the stricter way and relax it if we ever get a real user complaint about it, but happy to go along with whatever others think is best

I'd like to provide such a complaint. In ty, we would like to infer precise key-type specializations (unions of string literals) of dict_keys and dict_items for closed TypedDicts, where all possible keys are known. We have real user requests for this, because it allows iterating over dict keys/items and passing the keys along to a function that expects a limited set of literals.

But doing this causes false positives with isdisjoint, because the following valid code will now error:

class TD(TypedDict, closed=True):
    x: int

def check(td: TD) -> None:
    # Both operations are safe at runtime but will report invalid-argument-type:
    td.keys().isdisjoint(["other"])
    td.items().isdisjoint([("other", 1)])

This is really a sub-issue of https://github.com/python/typeshed/issues/15271, the same category of issue as #6597.

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 esaminando le annotazioni introdotte in PR 12309 e la discussione correlata in issue #15271. Riproduci i due esempi di TypedDict chiusi di questo issue, quindi modifica la tipizzazione dell'argomento is_disjoint in modo che entrambe le chiamate valide vengano accettate senza perdere i tipi precisi delle chiavi e degli elementi; verifica il comportamento risultante del controllo dei tipi.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
58/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.