> One possible phrasing of this: "Extra fields are not permitted in
literal assigments to TypedDicts, although at runtime they may be
present in TypedDict values."
I would certainly support this clarification in documentation related to
TypedDict.
In docs, sure.
As a completely separate idea, it may be useful to make it easy to
extract the extra field values from a runtime value of a TypedDict, and
to explicitly copy those field values to a new TypedDict instance.
Consider the following:
class Point2D(TypedDict):
x: int
y: int
class NamedPoint2D(Point2D):
name: str
n_point = NamedPoint2D(x=1, y=2, name='Center')
P = TypeVar('P', Point2D)
# Does swap the "x" and "y" components of a Point2D,
# while preserving its __extra__ fields.
def transpose(p: P) -> P:
return Point2D( # maybe write P( here instead?
x=p['y'], y=p['x'],
**Point2D.__extra__(p))
transposed_n_point = transpose(n_point)
print(transposed_n_point) # prints: {"x": 2, "y": 1, "name": "Center"}
reveal_type(transposed_n_point) # NamedPoint2D
The above code (1) uses a new @classmethod called __extra__() on the
Point2D TypedDict that extracts any fields from the provided value that
are not in Point2D's definition, and (2) allows a Point2D TypedDict to
be constructed with a ** splat with statically-unknown extra fields.
I’d like to see some use cases for such syntax before we go there. And why would __extra__ need to be a Point2D method?