python / python/mypy

Per type instance metadata

Aperta
#8,843 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

question topic-plugins
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

I've been working on pynamodb-mypy (started a while ago and coming back to it now...). The first task I took up is to implement attribute optionality, i.e.

from pynamodb.models import Model
from pynamodb.attributes import NumberAttribute

class MyModel(Model):
  my_number = NumberAttribute()
  my_optional_number = NumberAttribute(null=True)

reveal_type(MyModel().my_number)  # note: Revealed type is 'int'
reveal_type(MyModel().my_optional_number)  # note: Revealed type is 'Optional[int]'

I've been implementing:

  • get_function_hook to handle NumberAttribute initialization and parsing the 'null' parameter
  • get_attribute_hook to wrap the return type in optionality as needed

This required storing state between the function hook (parsing the arguments) and the attribute hook. One approach I've successfully tried is to append a "sentinel" type arg to the instance's args, i.e. from NumberAttribute it turns into NumberAttribute[None], and generally from SomeAttribute[...] to SomeAttribute[..., None] (I'm using a NoneType as the sentinel). Then in get_attribute_hook I'm looking for the sentinel.

You can see my current implementation here.

I was wondering what would be your thoughts on storing extra metadata on a type instance. Type args appear to be the only sanctioned "extras" as they're effectively part of the type. The new Annotated comes to mind, e.g. have my function hook return something like Annotated[AttrT, 'nullable_attr'], but I got the impression that it doesn't quite work this way -- that Annotateds should be converted to something else during semantic analysis rather than keeping them around for later stages to use.

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 leggendo pynamodb_mypy/plugin.py, in particolare get_function_hook e get_attribute_hook, quindi esamina la gestione da parte di mypy degli argomenti di tipo e di Annotated durante l’analisi semantica. L’issue sarà completa solo quando i maintainer avranno deciso e documentato un modo supportato per trasferire metadati per istanza relativi all’opzionalità; non sono stati forniti criteri di accettazione concreti.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.