[Feature] Use typing.Annotated for customization
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Review the existing converter-customization path and the proposed cattrs.annotations API, then compare it with typing.get_type_hints and the Annotated example in this issue. Done means the supported metadata behavior, backward-compatibility expectations, and implications for issues #352 and #552 are defined and verified.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
- cattrs version: 23.2.3
- Python version: 3.12
- Operating System: MacOS
Description
There's a decent amount of overhead in creating separate converters that needs to be done, which can get cumbersome quickly for a large number of classes. I was thinking it'd be possible to use typing.Annotated to reduce this work in a backwards-compatible way, without messing up type checking for those who need it.
An example that could solve (#352 and #552 if I understand them correctly)
from typing import Annotated
import cattrs.annotations as ca # hypothetically
class SomeKVPair:
key: Annotated[str, ca.Rename("Key")]
data_size: Annotated[int, ca.FromNested("Data.Size")]
value: Annotated[bytes, ca.AsBase64, ca.Rename("Value")] = b""
There are a few benefits to this:
- Type checkers will still understand the annotations.
- No need to rely on metadata inserted using
attrs.field()or similar. - It's clear to anyone looking at just the class what transforms are made. If they want to do their own serialization (e.g. create an XML serializer) it'll be clear what transforms they need to do.
- Getting the extra annotations is opt-in, so it'll be invisible to older versions of
cattrsthat don't support it. - Other libraries can also see these and use it for their own purposes.
- It's been available since 3.9. Version 3.8 goes EOL in October, so there's no need for compatibility shims.
Presumably this will be a non-trivial change, but I think it would be really valuable.
- Lingua principale
- Python
- Stelle
- 1.1k
- Fork
- 159
- Merge medio
- 12h 21m
- PR unite (30g)
- 6
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-attrs/cattrs
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python-attrs/cattrs#761 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
python-attrs/cattrs#513 · 4 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
python-attrs/cattrs#779 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 85/100
python-attrs/cattrs#774 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
python-attrs/cattrs#746 · 2 commenti ·
Tutte le issue di python-attrs/cattrs
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
stephrobert/dsoxlab#238 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
sublimehq/package_control#1780 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
nwg-piotr/nwg-displays#145 ·