[Feature] Use typing.Annotated for customization
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Review the existing converter-customization path and the proposed cattrs.annotations API, then compare it with typing.get_type_hints and the Annotated example in this issue. Done means the supported metadata behavior, backward-compatibility expectations, and implications for issues #352 and #552 are defined and verified.
由索引模型根据 Issue 内容生成。
描述
- cattrs version: 23.2.3
- Python version: 3.12
- Operating System: MacOS
Description
There's a decent amount of overhead in creating separate converters that needs to be done, which can get cumbersome quickly for a large number of classes. I was thinking it'd be possible to use typing.Annotated to reduce this work in a backwards-compatible way, without messing up type checking for those who need it.
An example that could solve (#352 and #552 if I understand them correctly)
from typing import Annotated
import cattrs.annotations as ca # hypothetically
class SomeKVPair:
key: Annotated[str, ca.Rename("Key")]
data_size: Annotated[int, ca.FromNested("Data.Size")]
value: Annotated[bytes, ca.AsBase64, ca.Rename("Value")] = b""
There are a few benefits to this:
- Type checkers will still understand the annotations.
- No need to rely on metadata inserted using
attrs.field()or similar. - It's clear to anyone looking at just the class what transforms are made. If they want to do their own serialization (e.g. create an XML serializer) it'll be clear what transforms they need to do.
- Getting the extra annotations is opt-in, so it'll be invisible to older versions of
cattrsthat don't support it. - Other libraries can also see these and use it for their own purposes.
- It's been available since 3.9. Version 3.8 goes EOL in October, so there's no need for compatibility shims.
Presumably this will be a non-trivial change, but I think it would be really valuable.
- 主要语言
- Python
- 星标
- 1.1k
- 派生
- 171
- 平均合并
- 14 小时 46 分钟
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/cattrs 的其他 Issue
-
Dataclass default_factory field treated as required in union disambiguation可能已有人在做 @Eryx-Z 于 3 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
python-attrs/cattrs#796 ·
维护者通常 1 天内回复
-
decimal.Decimal not handled in preconf converters (json, pyyaml)可能已有人在做 @Binilkks 于 114 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
python-attrs/cattrs#761 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
python-attrs/cattrs#513 · 4 条评论 ·
维护者通常 1 天内回复
-
Structure hook on a PEP 695 alias is ignored inside a passthrough union可能已有人在做 @Eryx-Z 于 3 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 62/100
python-attrs/cattrs#797 ·
维护者通常 1 天内回复
-
msgspec converter skips hooks and drops private fields in nested recursion可能已有人在做 @Gabrieljr42 于 3 天前认领。 未关闭
难度 3/5 半天 新手友好度 64/100
python-attrs/cattrs#795 · 1 条评论 ·
维护者通常 1 天内回复
查看 python-attrs/cattrs 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 90/100
SublimeText/AdvancedNewFile#211 ·
-
难度 2/5 1-3 小时 新手友好度 73/100
-
难度 2/5 1-3 小时 新手友好度 83/100
Imbad0202/academic-research-skills#987 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
-
Pull-through from gcr.io returns 500 when pulling using podman/buildah可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭Issue Triage-Needed
难度 2/5 1-3 小时 新手友好度 68/100
pulp/pulp_container#2581 ·
维护者通常 1 天内回复