Indeed I am remembering incorrectly. According to mypy, you cannot write the value of an unknown key:
# td_setitem_unknown_key.py
from typing import TypedDict
class Point2D(TypedDict):
x: int
y: int
# unknown keys must not be ReadOnly because this is a TypedDict
class Example(TypedDict, Point2D):
w: str
w: Example = { "x": 1, "y": 2, "w": "foo" }
p: Point2D = w
p['w'] = 0 # error: TypedDict "Point2D" has no key "w" [typeddict-unknown-key]
…however you can read the value of an unknown key with get
, since there might actually be a value (from when previously manipulated as a subclass):
# td_get_unknown_key.py
from typing import TypedDict
class Point2D(TypedDict):
x: int
y: int
# unknown keys must not be ReadOnly because this is a TypedDict
class Example(TypedDict, Point2D):
w: str
w: Example = { "x": 1, "y": 2, "w": "foo" }
p: Point2D = w
print(p.get('w')) # OK
I’ll edit away blur my discussion which relies on this misremembering.