106 words
1 minute
typing feature - typedDict

来自 typing(Python 3.11+)或 typing_extensions(旧版本),作用是:
在
TypedDict里把某个字段标记为 可选(不是必须提供)
示例
from typing import TypedDict, NotRequired
class User(TypedDict): name: str # 必填 age: NotRequired[int] # 可选这样写就允许:
u1: User = {"name": "Tom"} # ✅ oku2: User = {"name": "Tom", "age": 18} # ✅ ok如果不用 NotRequired(默认都是必填)
class User(TypedDict): name: str age: int那么:
{"name": "Tom"} # ❌ 类型检查会报缺少 age typing feature - typedDict
https://lxy-alexander.github.io/blog/posts/python/typing-feature---typeddict/