You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type of "ex" is "QuickExample"
Type of "ex.__getitem__("key1")" is "object"
Type of "ex["key1"]" is "int"
Type of "ex.__getitem__" is "(__key: str, /) -> object"
VS Code extension or command-line
Initially pylance, also confirmed on the playground
The text was updated successfully, but these errors were encountered:
The _TypedDict type in typing.pyi defines the class as deriving from Mapping[str, object], and the __getitem__ method comes from the Mapping class. Neither pyright nor mypy synthesize overrides for the TypedDict __getitem__ method. FWIW, mypy is the reference implementation for PEP 589, which introduced TypedDicts.
Both pyright and mypy handle special-casing for TypedDict indexing at a higher level than __getitem__. In the case of pyright, this is especially important because pyright also supports narrowing of TypedDict keys based on existence checks.
classQuickExample(TypedDict, total=False):
key1: intkey2: strdeffunc(x: QuickExample):
print(x["key1"]) # Error: "key1" is not a required keyif"key1"inx:
print(x["key1"]) # No type error
This is done by internally tracking which keys in the TypedDict have been verified — and are therefore guaranteed to exist. This analysis wouldn't be possible if the type checker synthesized static __getitem__ methods.
That's unfortunate, as it breaks reasonable assumptions about function composition. I'll just add this to the growing list of "things to bring up with the typing council" since you've placed this as due to the definition coming from Mapping
Describe the bug
A clear and concise description of the behavior you are seeing and the expected behavior along with steps to reproduce it.
Code or Screenshots
and the corresponding informational messages:
VS Code extension or command-line
Initially pylance, also confirmed on the playground
The text was updated successfully, but these errors were encountered: