Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Natively support more types with pre-defined optional hooks

未关闭
#641 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 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 内容生成。

描述

enhancement

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:

  1. 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
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)
  1. This new function could be even generalized to accept multiple types, like
register_extra_hooks(IPv4Address, ZoneInfo)
  1. 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.

  2. 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-attrs/cattrs 的其他 Issue

查看 python-attrs/cattrs 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。