Skip to content

Pyright handling items method of TypedDict #9162

Answered by erictraut
mheguy asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, pyright is working correctly here. The way TypedDict is defined in the typing spec, it is a structural type, and a value that is compatible with a TypedDict can contain additional items beyond what that TypedDict defines. That means when iterating over all items, a type checker needs to assume there are potentially additional items present whose values are object type.

If you know that there are no additional items present, you can add an assert isinstance(bloop, str) statement in your loop.

You may also be interested in draft PEP 728, which proposes to add support for "closed" TypedDict definitions. This PEP is in flux, but pyright has provisional support for the latest draft if yo…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mheguy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants