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
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 of T2 = Mapping[str, Union[v_1, v_2, ... v_n]]. However, this causes a problem when combined with the structural subtyping rule that T3 = TypedDict[ {k_1: v_1, k_2: v:2, ... k_n: v:n, k': v'} ] is a subclass of T1, which implies that T3 is a subclass of T2. When you call __getitem__ on a T2 that's actually a T3 with key k', you would unexpectedly get a v'. This can lead to an undetected potential AttributeError as in the following example:
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 Mapping[str, object] as a supertype. This would be safer, but it may require users to add some extra casts.
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: