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
After we support optional items (#2632), the join of two typed dicts should perhaps have the combined set of items from both dicts, but items not in both dicts would be optional. For example, consider these typed dicts:
A = TypedDict('A', {'x': int, 'y': str})
B = TypedDict('B', {'x': int, 'z': str})
Now I think that the join of A and B should be this:
This wouldn't be be type-safe, since it's possible that a value with type A also has a runtime 'z' key with an incompatible value type such as int, but this is not visible to the type checker due to structural subtyping. This doesn't feel like a huge problem to me, but it's something to keep in mind.
Consider a concrete example where this would be useful:
B = TypedDict('B', {'a': int})
A = TypedDict('A', {'x': int, 'y': OptionalItem[B]})
a: A = ...
a.get('y', {})
Now the type of the final expression would be {'a': OptionalItem[int]} -- the result of joining B and an empty typed dict {}. For this to work in general, union simplification also needs to use joins when combining two items.
An alternative approach would be just special case a {} argument to get to produce the same result as this proposal, and not do this elsewhere where it would be unsafe to do so. This would be a little less flexible, but maybe the benefits of the general solution would not be worth the unsafety. We could also start with this restricted approach and only introduce the general solution if we find evidence that special casing get is not good enough in real code.
The text was updated successfully, but these errors were encountered:
I no longer think that this is a good idea since this is not type safe. Closing this -- we can reconsider if it seems that this would be very useful for type checking real code.
After we support optional items (#2632), the join of two typed dicts should perhaps have the combined set of items from both dicts, but items not in both dicts would be optional. For example, consider these typed dicts:
Now I think that the join of
A
andB
should be this:This wouldn't be be type-safe, since it's possible that a value with type
A
also has a runtime'z'
key with an incompatible value type such asint
, but this is not visible to the type checker due to structural subtyping. This doesn't feel like a huge problem to me, but it's something to keep in mind.Consider a concrete example where this would be useful:
Now the type of the final expression would be
{'a': OptionalItem[int]}
-- the result of joiningB
and an empty typed dict{}
. For this to work in general, union simplification also needs to use joins when combining two items.An alternative approach would be just special case a
{}
argument toget
to produce the same result as this proposal, and not do this elsewhere where it would be unsafe to do so. This would be a little less flexible, but maybe the benefits of the general solution would not be worth the unsafety. We could also start with this restricted approach and only introduce the general solution if we find evidence that special casingget
is not good enough in real code.The text was updated successfully, but these errors were encountered: