Per type instance metadata
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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_hookto handle NumberAttribute initialization and parsing the 'null' parameterget_attribute_hookto 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem Lesen von pynamodb_mypy/plugin.py, insbesondere von get_function_hook und get_attribute_hook, und sehen Sie sich anschließend mypys Umgang mit Typargumenten und Annotated während der semantischen Analyse an. Das Issue ist erst abgeschlossen, wenn die Maintainer einen unterstützten Weg beschlossen und dokumentiert haben, um Metadaten pro Instanz für Optionalität zu übertragen; konkrete Akzeptanzkriterien sind nicht vorgegeben.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 18/100