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
Feature request: track the common type in a TypedDict.
It has been established (#4976 ) that TypedDict is compatible with Mapping[str, Any] (although not with Dict) because it hides mutating operations. However, it is not compatible with any more specific Mapping Types, even though it clearly has all the information needed to identify the possible value types.
class T(TypedDict):
a: int
b: int
def foo(x: Mapping[str, int]):
...
def bar(y: Mapping[str, str]):
...
def qux(z: Mapping[str, Any]):
...
s = {"a": 5, "b": 3}
foo(s)
bar(s)
qux(s)
t: T = {"a": 5, "b": 3}
foo(t)
bar(t)
qux(t)
Expected behaviour:
bar(s) and bar(t) should fail.
qux(s) and qux(t) should pass.
foo(s) and foo(t) should pass.
Observed behaviour (MyPy 0.780 on Windows):
bar(s) and bar(t) fail.
qux(s) and qux(t) pass.
foo(s) passes but foo(t) fails.
A TypedDict can safely be considered a Mapping from str to the Union of the types of the presented entries. A significant implication of this is that it is safe to iterate over a TypedDict and use information common to all represented types.
The text was updated successfully, but these errors were encountered:
This is intentional, TypedDicts behave as Mapping[str, object] to be safe w.r.t. structural subtyping, namely TypedDict({'x': X, 'y': Y, 'z': Z}) is a subtype TypedDict({'x': X, 'y': Y}).
I see! Should have read the PEP instead of just the docs.
I took "expects all of its instances to have a certain set of keys" to mean that the set of keys was fixed: all (except as explicit with the totality flag) and only those keys would feature and all would have the corresponding type.
I still think that this would be useful if it could be supported, perhaps by interaction with the final decorator or something similar to imply the set of keys is indeed exhaustive . However, that would clearly take rather more thought.
Feature request: track the common type in a TypedDict.
It has been established (#4976 ) that TypedDict is compatible with Mapping[str, Any] (although not with Dict) because it hides mutating operations. However, it is not compatible with any more specific Mapping Types, even though it clearly has all the information needed to identify the possible value types.
Expected behaviour:
bar(s) and bar(t) should fail.
qux(s) and qux(t) should pass.
foo(s) and foo(t) should pass.
Observed behaviour (MyPy 0.780 on Windows):
bar(s) and bar(t) fail.
qux(s) and qux(t) pass.
foo(s) passes but foo(t) fails.
A TypedDict can safely be considered a Mapping from str to the Union of the types of the presented entries. A significant implication of this is that it is safe to iterate over a TypedDict and use information common to all represented types.
The text was updated successfully, but these errors were encountered: