Natively support more types with pre-defined optional hooks
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- python
- Domain
- backend-api-design
Research direction
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.
Written by the indexing model from the issue text.
Description
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?
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 11h 51m
- Merged PRs (30d)
- 7
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-attrs/cattrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-attrs/cattrs#761 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
python-attrs/cattrs#779 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
Maintainers usually reply within 1 day
All issues in python-attrs/cattrs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-2 days Newbie friendliness 70/100
-
FingerprintSplitter raises ZeroDivisionError when int(frac_train * len(dataset)) floors to zeroOpen
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 7 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
lmstudio-ai/mlx-engine#376 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pyiron/bagofholding#166 ·