-
Heya, the behaviour of the following is unexpected: from typing import TypedDict
class TestClass(TypedDict):
input: str
for _, bloop in TestClass(input="input").items():
_.lower()
bloop.lower() # error: Cannot access attribute "lower" for class "object" - Attribute "lower" is unknown (reportAttributeAccessIssue) This doesn't seem like a bug in Pyright, it's just doing what the type hints say. Is there a (reasonable) way to work around this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yeah, pyright is working correctly here. The way If you know that there are no additional items present, you can add an 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 you would like to play with it. You need to set |
Beta Was this translation helpful? Give feedback.
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 areobject
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…