-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsoundness in subclassing Mappings with TypedDicts #5927
Labels
Comments
Yeah, we are aware of the problem -- and I actually started working on a fix earlier today :-). I'm trying out what happens if make all typed dicts have |
JukkaL
added a commit
that referenced
this issue
Nov 21, 2018
The previous fallback mapping item type was join of the value types, which is unsafe because of structural subtyping. Fixes #5927.
JukkaL
added a commit
that referenced
this issue
Nov 21, 2018
The previous fallback mapping item type was join of the value types, which is unsafe because of structural subtyping. Fixes #5927.
JukkaL
added a commit
that referenced
this issue
Nov 22, 2018
The previous fallback mapping item type was join of the value types, which is unsafe because of structural subtyping. Fixes #5927.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the course of implementing TypedDicts into Pyre, I came across an issue with how mypy is treating the relationship between TypedDicts and Mappings.
It seems like the implemented semantics are to treat a
T1 = TypedDict[ {k_1: v_1, k_2: v:2, ... k_n: v:n} ]
as a subclass ofT2 = Mapping[str, Union[v_1, v_2, ... v_n]]
. However, this causes a problem when combined with the structural subtyping rule thatT3 = TypedDict[ {k_1: v_1, k_2: v:2, ... k_n: v:n, k': v'} ]
is a subclass ofT1
, which implies thatT3
is a subclass ofT2
. When you call__getitem__
on aT2
that's actually aT3
with keyk'
, you would unexpectedly get av'
. This can lead to an undetected potential AttributeError as in the following example:This passes mypy typechecking but when run gives an attribute error.
I think we're going to end up having TypedDicts just subclass
Mapping[str, Any]
for this reason.The text was updated successfully, but these errors were encountered: