Improvements to .get(...) on TypedDict with Literals #3292
Labels
addressed in next version
Issue is fixed and will appear in next published version
enhancement request
New feature or request
Is your feature request related to a problem? Please describe.
This is somewhat related to #1058
I have a similar situation where i have multiple
TypedDict
s in a union, and only one of which holds a key.Something like:
Which type-checks fine, as it should, however we'll find if we change the return type of
test
to anything (barNoReturn
) the code will still type-check, even though I would assume most developers would expect an error to occur.I understand how pyright currently handles the situation, falling back to the
(str) -> Any
overload, if the key isn't matched in a part of the union.So in the example case
c.get('foo')
:(k: Literal['foo']) -> int | None
forA
(this in itself seems like another thing which could be improved? if theTypedDict
istotal
or the key isRequired
this can't beNone
)(k: str) -> Any | None
forB
which flatten to
int | Any | None
Describe the solution you'd like
Is there any reason why if the
TypedDict
istotal
and the argument to.get
is aLiteral
it makes sense for thestr
fallback to be reached in the first place? if that was omitted we'd end up with the expectedint | None
here.There may be some case where subclassing of the types would cause issues? as a counter to that I would wonder, if the previous statement isn't true whether decorating
A
andB
with@final
would alleviate the issue?There's possibly some other argument to be had about
Any
being used here at all, since if the types of all keys are known,.get(...)
really should only be able to return aUnion
of those value types andNone
.Thoughts?
The text was updated successfully, but these errors were encountered: