You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 Converter method register_extra_hooks(converter, cls, structure=True, unstructure=True) (and its variant without first argument, defaulting to global_converter) that will cover less frequently used types from stdlib, like uuid, zoneinfo, ipaddress, re.Pattern, etc? This won't require any overhead in default case, but will enable one liner
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 using inspect.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?
The text was updated successfully, but these errors were encountered:
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:
Converter
methodregister_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 linerinstead of
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?
The text was updated successfully, but these errors were encountered: