02 · 类型系统:注解由谁消费
结论:解释器只把注解存进 __annotations__,谁读、怎么用由消费者决定——@dataclass 只当字段清单,Pydantic 在类创建时构建验证树。
%%{init: {"flowchart": {"wrappingWidth": 210}}}%%
flowchart LR
A["def f(x: int) -> str<br/>class User: id: int"] --> ANN["__annotations__<br/>解释器只存、不看"]
ANN --> S["静态消费:mypy / pyright<br/>不运行代码"]
ANN --> D["@dataclass<br/>只读字段名与顺序,exec 生成 __init__"]
ANN --> P["Pydantic / FastAPI / beartype<br/>get_type_hints() 读出来,运行时校验"]
classDef hot fill:#fde68a,stroke:#b45309
class P hot
COMMENTS
评论存放在 GitHub Discussions, 用 GitHub 账号登录即可发表,支持 Markdown。 想针对正文某句话说?选中那段文字,点浮出的「评论」即可划线评论;觉得哪里写错了,发表时勾上「同时提交 Issue」。 有人回复你时 GitHub 会按你的通知设置发邮件,不用守在这里。