Natively support more types with pre-defined optional hooks
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 30/100
- Issue 类型
- 功能
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- python
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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?
- 主要语言
- Python
- 星标
- 1.1k
- 派生
- 159
- 平均合并
- 12 小时 21 分钟
- 30 天内合并 PR
- 6
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/cattrs 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
python-attrs/cattrs#761 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 64/100
python-attrs/cattrs#513 · 4 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 35/100
python-attrs/cattrs#779 ·
-
难度 3/5 1-2 天 新手友好度 85/100
python-attrs/cattrs#774 ·
-
难度 5/5 一周以上 新手友好度 35/100
python-attrs/cattrs#746 · 2 条评论 ·
查看 python-attrs/cattrs 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
stephrobert/dsoxlab#238 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
sublimehq/package_control#1780 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 70/100
nwg-piotr/nwg-displays#145 ·