-
One of my favorite new constructs since 3.12 is a generic, but homogenious TypedDict like this: class StructuredMap[T](TypedDict):
a: T
b: T My problem is that, in contrast to a regular dict, this construct does not propagate its type specialization to In other words, what i want to define is a regular dict (with homogeneous value types), but with pre-defined keys. Is there a way to annotate such a construct? Am I thinking too complicated? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
There's currently no way to define that in the type system. There is a draft PEP 728 that would allow for this. The reason that a type checker cannot narrow the type of |
Beta Was this translation helpful? Give feedback.
There's currently no way to define that in the type system. There is a draft PEP 728 that would allow for this. The reason that a type checker cannot narrow the type of
values
anditems
with today'sTypedDict
is because other (unspecified) keys may be present in a class that is compatible with aTypedDict
. Since these potential other keys have values of an unknown type, they need to be treated asobject
. PEP 728 would allow you to specify that no other keys can be present.