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
{{ message }}
Repository navigation
"Expected TypedDict key to be string literal" false positive? #4122
Hm, the missing line number is a bug. The error is clearly about the final line of your example.
Unfortunately mypy isn't smart enough to understand that in this particular case the result will have exactly the desired set of keys. It basically gives up as soon as it sees {**expr}. Maybe we could teach it to calculate the minimal and maximal sets of keys for such usage (if **expr references a TypedDict) and give better diagnostics based on that.
I believe there are many other ways of constructing a TypedDict that won't be accepted; I think the best way is to just use a cast or put # type: ignore on it.
Reacted by Nikolay Shebanov, Jón Levy, Andrew Chang-DeWitt, Zhuyi Xue and Alireza Jafari
This is still an issue in 0.782. Has anything changed since 0.530 that could make the type detection for this case feasible? I.e. the discussion in #4441 seems relevant.
My current workaround is to use cast. A bit better than # type: ignore.
Reacted by Andrew Chang-DeWitt, Alexander Dmitriev, Eduardo Côrtes, Flávio Lisbôa, Matteo Bernardini, Patricio Serrano, Shuang Wu, malmiteria and pgr1c11
Activity
gvanrossum commentedon Oct 16, 2017
Hm, the missing line number is a bug. The error is clearly about the final line of your example.
Unfortunately mypy isn't smart enough to understand that in this particular case the result will have exactly the desired set of keys. It basically gives up as soon as it sees
{**expr}. Maybe we could teach it to calculate the minimal and maximal sets of keys for such usage (if**exprreferences a TypedDict) and give better diagnostics based on that.I believe there are many other ways of constructing a TypedDict that won't be accepted; I think the best way is to just use a cast or put
# type: ignoreon it.ilevkivskyi commentedon Oct 16, 2017
The missing line number should be easy to fix, but the
{**expr}type detection might be harder.killthekitten commentedon Jul 9, 2020
This is still an issue in
0.782. Has anything changed since0.530that could make the type detection for this case feasible? I.e. the discussion in #4441 seems relevant.My current workaround is to use
cast. A bit better than# type: ignore.Support flexible TypedDict creation/update (#15425)