Allow use of `Required` and `NotRequired` to make an existing typed dict total or optional

Aperta
#1,454 8 commenti 7 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

Inizia esaminando la semantica proposta di TypedDict, Required e NotRequired e gli esempi nell’issue, inclusi l’ereditarietà e le annotazioni inline. Definisci il comportamento e identifica la typing specification pertinente e i test di conformità; il lavoro è completato quando le trasformazioni richieste sono specificate e validate in modo coerente.

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

Descrizione

topic: feature

I think the ability of indicating that a typed dict requires all keys or that all are optional would be very useful.

For example to allow defining default values for a particular dict or to return a dict that has been completely assigned from a function

A simple example

class A(TypedDict):
    a: int
    b: int

ADefault = {'a': 1, 'b': 42}

def optionalA(v: NotRequired[A]) -> A:
    return ADefault | v

optionalA({'a': 11}) # ok since all keys of NotRequired[A] are optional

class X(TypedDict, total=False):
    x: str
    y: str

XDefault: X = {"x": "xv", "y": "yv"}

def withXDefault(v: X) -> Required[X]:
    return XDefault | v

withXDefault({"x": "foo"})["y"]  # ok since all keys of Required[X] are required

This idea is inspired by the analogous types in typescript Required and Partial that are in general very convenient.

I think it makes sense having the same behaviour of the typescript counterparts:

  • NotRequired[X] is a no op, since X is defined as total=False. Same for Required[A].
  • Required/NotRequired would include all keys of the typed dict and all its superclasses. This differs from how total works. Example:
    class TD1(TypedDict):
        a: int
    
    class TD2(TypedDict, total=False):
        b: int
    
    TDR = Required[TD2]  # b becomes required
    TDNR = NotRequired[TD2]  # a becomes not required
    
  • Required/NotRequired used inline in the TypedDict are not taken into consideration, so the end result is the same independently of how the typed dict is defined
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

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.

Altre issue di python/typing

Tutte le issue di python/typing

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.