-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
in
keyword should narrow Union[TypedDict, ...]
#9953
Comments
You can get around this by giving each typeddict a unique tag but it would be nice not to have to do this. TypeScript has this feature "The |
indeed this would be great! having mypy errors i should not have because of this |
Since #13838 this already works, but you have to decorate the TypedDicts with from typing import final
@final
class MovieResponse(TypedDict):
...
@final
class BookResponse(TypedDict):
... since otherwise you can't rule out class BookBasedOnMovieResponse(BookResponse):
movie: Movie @AlexWaygood we could close this issue, but I do think we could better document it at the very least (#15695). p.s. I also wonder if mypy should surface a hint, e.g.
|
Thanks @ikonst indeed this worked! |
FWIW we're considering removing the need to add |
Feature
Using
in
on aUnion[TypedDict]
should narrow theUnion
. There is already something similar forisinstance
that can narrow based on types. If a certain key exists you could narrow down that your types from a Union as well. This would prevent an error saying a certain key is missing because you checked already, they code can safely access the key. Currently the only workaround for this would be to usecast()
but really shouldn't be necessary.Pitch
After the change the following code should pass validation.
The text was updated successfully, but these errors were encountered: