Jelle recently merged some updates into the PEP 728 draft spec. Most of these updates make sense to me, but there is one clarification that surprised me. I’d like to get feedback on it. The latest draft says:
Extra keys included in
TypedDictobject construction should still be caught…
The “still” is referring to the fact that the typing spec indicates that when constructing a TypedDict object — which is typically done through a dict literal expression, the presence of extra items is flagged as an error.
class NoExtra(TypedDict):
a: str
v1: NoExtra = {"a": "", "b": 42} # Error
However, I’m not sure it makes sense to extend this rule to TypedDicts with extra_items specified.
class Extra(TypedDict, extra_items=int):
a: str
v2: Extra = {"a": "", "b": 42} # Error?
If someone has gone through the trouble of defining a TypedDict with extra_items, it seems to me that we should allow extra items to be supplied in a dict literal expression as long as the values are of a compatible type. What do others think?
On a related topic, the PEP is not clear about the behavior when a TypedDict with extra_items is used to annotate a **kwargs parameter. Should additional keyword arguments be allowed in this case? This came up in a recent pyright bug report.
def no_extra(**kwargs: Unpack[NoExtra]): ...
def extra(**kwargs: Unpack[Extra]): ...
no_extra(a="", b=42) # Error (according to typing spec)
extra(a="", b=42) # Error?
My sense is that the second call (to extra) should not result in a type error. Do others agree?
In any case, I think the answer to these two questions (about dict literals and keyword arguments) should be consistent. That is, we should either allow both or disallow both.