python / python/typing

Allow creating type aliases with type checked metadata attached

Aperta
#555 6 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Annotations in Python have many use cases beyond static type checking. I'd really like to be able to create a type that is an alias to another type while also attaching metadata to it. The idea is to be able to use MyPy's type system but also use annotations for other use cases such as runtime validation at the same time. I'm thinking something along these lines (though I have no idea how this would work in practice):

from datetime import datetime
from mypy_extensions import TypedDict
from somewhere import AliasWithMetadata

# define metadata type
Options = TypedDict('Options', {'min': int, 'max': int})
# define type alias with allowed metadata
MinMaxInt = AliasWithMetadata(int, Options)

# define age arg as type int with metadata attached
def get_rough_birth_year(age: MinMaxInt(min=0, max=120)):
    return datetime.today().year - age

# call function with int without mypy complaining
get_rough_birth_year(105)

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

Non sono identificati file, test o punti di ingresso per l’implementazione. Inizia esaminando l’esempio proposto di AliasWithMetadata e il comportamento esistente di Python typing e MyPy, quindi chiarisci la semantica richiesta dei controlli statici e dei metadati a runtime. Per considerarlo completato, sarebbero necessari un design concordato e un comportamento documentato per la definizione e l’uso di alias di questo tipo.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.