python / python/typing

Discrepancy between spec and tests on indexing TypedDicts with unknown literal keys

Aperta
#2,054 0 commenti 0 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

Splitting issue from PR https://github.com/python/typing/pull/1978

Spec says that indexing with unknown literal keys should be reported as an error:

The use of a key that is not known to exist should be reported as an error, even if this wouldn’t necessarily generate a runtime type error. These are often mistakes, and these may insert values with an invalid type if structural assignability hides the types of certain items. For example, d[‘x’] = 1 should generate a type check error if ‘x’ is not a valid key for d (which is assumed to be a TypedDict type).

But it's not reflected in conformance tests (https://github.com/python/typing/blob/2d88da2a407b556f5b52b4c6ad1bd659873bb9ac/conformance/tests/typeddicts_operations.py) and there's a bit of discrepancy between type checkers how they handle some cases (most of them reject all cases that may cause runtime error / allow cases that do not cause runtime error ).

Would be great if we can either adapt conformance tests to the spec or amend the spec to remove the ambiguity.

Related discussion - https://discuss.python.org/t/typeddict-operations-with-unknown-literal-keys/89653 (also has information on state of this issue in different type checkers).

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

Confronta il requisito citato della specifica con conformance/tests/typeddicts_operations.py, quindi esamina la discussione collegata e la PR correlata per analizzare il comportamento diverso dei type-checker. Determina se devono essere modificati i test di conformità o la specifica; il lavoro è concluso quando l’ambiguità è risolta e la specifica e i test concordano.

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

Valutazione

Stack tecnologico
python
Ambito
documentation, testing
Tipo di issue
Bug
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.