Natively support more types with pre-defined optional hooks
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Da chiarire
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- backend-api-design
Direzione di ricerca
Start by reviewing the Converter hook-registration APIs and global_converter, then compare the proposed register_extra_hooks variants for stdlib types such as uuid, zoneinfo, ipaddress, and re.Pattern. Done means the scope and registration behavior are agreed, including whether multiple types and third-party hooks are supported; no test path is named in the issue.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
cattrs comes with a rich set of converters, but the variety of types supported by those converters out of the box is limited. Adding custom structure/unstructure hooks is pretty straightforward and well documented, and in most cases could be as trivial as in the example below. However, as an end user, I would be happy to have something even more simple, something that I wouldn't copy-paste from one project to another :)
A few ideas come to my mind:
- What about adding a standalone function or a
Convertermethodregister_extra_hooks(converter, cls, structure=True, unstructure=True)(and its variant without first argument, defaulting toglobal_converter) that will cover less frequently used types from stdlib, likeuuid,zoneinfo,ipaddress,re.Pattern, etc? This won't require any overhead in default case, but will enable one liner
register_extra_hooks(converter, ZoneInfo)
instead of
@converter.register_structure_hook
def structure_zoneinfo(value: Any, _) -> ZoneInfo:
return ZoneInfo(str(value))
@converter.register_unstructure_hook
def unstructure_zoneinfo(value: ZoneInfo) -> str:
return str(value)
- This new function could be even generalized to accept multiple types, like
register_extra_hooks(IPv4Address, ZoneInfo)
-
It is possible to support non-stdlib modules in this function, by e.g. checking
__module__string or usinginspect.getmodule()before importing third-party module itself. -
Extra hooks could be registered by third-party libraries with e.g.
pluggy.
Does anything of the mentioned above sound reasonable?
- 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
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·