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
{{ message }}
Repository navigation
final TypedDict with total=False does not allow clear/popitem #14914
PEP-589 disallows clear and popitem on TypedDicts, even if all known keys are not required, as there may be other required keys due to structural subtyping. However, a @final-decorated TypedDict cannot be structurally subtyped, so these methods should be available.
Normally it would have to be the first one, but because the TypedDict is final, we know the complete possible set of key/value types. The third would be the most accurate but also the most costly to work with and hardest to read if any errors came up.
I'd personally go with option (2). Silently inferring Any would feel pretty unfortunate here, in my opinion, given that we've got enough information to infer a better type. But the third option feels unnecessarily complex, at least for now.
Activity
AlexWaygood commentedon Mar 17, 2023
Kiiiiiind of a duplicate of #7981, in that mypy doesn't currently have any special handling for
TypedDicts decorated with@final:)alicederyn commentedon Mar 17, 2023
Actually it does now! See #13838
AlexWaygood commentedon Mar 17, 2023
Oh my bad! Thank you!
alicederyn commentedon Mar 17, 2023
What return type should
popitemhave?Tuple[str, Any]Tuple[str, Union[ValueType1, ValueType2, ...]]Union[Tuple[Literal["key1"], ValueType1], Tuple[Literal["key2"], ValueType2], ...]Normally it would have to be the first one, but because the TypedDict is final, we know the complete possible set of key/value types. The third would be the most accurate but also the most costly to work with and hardest to read if any errors came up.
AlexWaygood commentedon Mar 17, 2023
I'd personally go with option (2). Silently inferring
Anywould feel pretty unfortunate here, in my opinion, given that we've got enough information to infer a better type. But the third option feels unnecessarily complex, at least for now.