-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Narrowing Union[TypedDict] with in
keyword
#11080
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
Comments
The condition It sounds like you want some form of conditional type narrowing where mypy determines "if Also, since your classes if 'a' in x:
reveal_type(x) # A | B (because it's not safe to narrow) If you mark both if 'a' in x:
reveal_type(x) # A I recently added support for this form of narrowing in pyright, but it doesn't appear to be implemented yet in mypy. There's some discussion of it here. |
Hi Eric, thanks for the quick response and explanation. I didn't know this would be tricky feature. There was a similar feature request from 2019 and it was implemented in this pr. After reading your explanation, I think what makes my use case complex is Since it's diffcult to do type narrowing here, I'm thinking about poential workaround. Can mypy check whether Also thanks for pointing out the potential subclass issue. I just did some experiment and it lookslike mypy doesn't allow me to apply Great to know pyright supports narrowing for literal key checking. I really hope mypy will add the same feature |
There are two ways I can think to implement what you're suggesting. The first is what I mentioned above. Mypy would need to track expression types that are conditioned on the types of other expressions. And it would need to "kill" those conditions if the dependent type changes (e.g. if Another potential approach is for mypy to analyze the entire method multiple times when the annotated parameter types are unions. It would need to do this combinatorially. In your example, it would need to analyze
While this approach would work, it has some major downsides: 1) it would significantly slow down type analysis — exponentially in cases where functions accept many parameters with unions, 2) it would produce multiple redundant error messages, once for each pass, 3) it would be a pretty major overhaul of the type checker logic. |
Thanks a lot for sharing your insights into type analyser. I will leave this issue open for a few days, in case someone else is also interested. |
Closed as it's too difficult to implement |
Feature
mypy should narrow Union[TypedDict] when using
in
keyword.Pitch
The example above triggers errors in mypy:
In theory,
if key in x
condition should provide enough information for mypy to narrow the type of parameterx
. It should know thatx
is aligned withkey
, so the print statement is type safe.Current workaround is to manually cast the type as below,
but when there's much more keys and TypedDicts involved, this approach would become verbose and messy.
If mypy can add this narrowing feature, it will allow more flexibility for TypedDict Union use cases
The text was updated successfully, but these errors were encountered: