from typing import TypedDict, NotRequired
class Address(TypedDict):
street: str
city: str
zip: str
class User(TypedDict):
id: int
name: str
email: str
address: NotRequired[Address] # Python 3.11+
def get_user(uid: int) -> User:
return {"id": uid, "name": "Alice", "email": "a@x.com"}
u = get_user(42)
print(u["name"]) # type-checked: str
# u["age"] # mypy error: not in User
Create a free account and build your private vault. Share publicly whenever you want.