-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix is_overlapping_types(NoneTyp, AnyType) for strict Optional checking #2973
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
Conversation
This needs to be reviewed by @ddfisher. |
Thanks for your contribution! Your alternative fix looks good to me -- it's theoretically correct, and it doesn't cause any problems when run over a large quantity of existing annotated code. That said, the fact that we didn't previously check for @JukkaL: do you know if there's any particular reason we don't currently check for |
I suspect that the reason we don't check for Example: However, it's possible that in some contexts we want a different definition of overlapping. What I described above could be called "known to overlap"; however, we might also want to determine whether two types "might overlap", and in the latter case I can look at this more carefully if the above explanation doesn't help with this PR. |
@JukkaL Ah, interesting! It looks like, according to the docstring, |
The docstring may not tell the whole story. Anyway, there seems to be no reason to make this change only for strict optional. If it's safe to do, it will be safe to do it for non-strict-optional code as well. There are only a small number of test cases that use strict optional checking so only having your change enabled for strict optional bypasses almost all tests, unfortunately. If doing it unconditionally causes test failures, it's likely that the change is not correct and we need to find a different fix. If tests still pass, it might be good, but I'd still like to have a look at all uses of |
Note that even if |
Okay I did some experimentation and your idea seems correct -- |
OK, done! |
Thanks! |
This fixes #2970. However, this is my first contribution to mypy, so I don't really know if this is the right place to fix it. (I'm mostly going off of following the control flow back from
visit_assert_stmt
and noticing that the comment near here seemed wrong.)Note: I defaulted to a conservative change (only to the
if experiments.STRICT_OPTIONAL
branch of the function). However, I noticed that the function doesn't explicitly check whether either side isAny
, instead leaving this path to be caught by the catch-all at the end. This seems like it could lead to other similar bugs in the future if people forgot aboutAny
, and so an alternative fix might be to check if either argument isAny
before doing anything else. I've left that change commented out so you can see what I'm talking about.Let me know which is preferable! And sorry if I've missed something basic.