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"} # ✅ ok
u2: 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/
Author
Alexander Lee
Published at
2026-01-26
License
CC BY-NC-SA 4.0