-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy 0.981 does not simplify unions for assert_type #13760
Comments
Thanks for the report! |
Here is a reduced repro: from typing import Union
from typing_extensions import assert_type
Scalar = Union[int, bool, bytes, bytearray]
def reduce_it(s: Scalar) -> Scalar:
return s
assert_type(reduce_it(True), Scalar) Interestingly, this requires two pairs of subclasses in the Union to repro; if I just put |
Yeah, the change was kind of intentional. I wanted mypy to check something like How much inconvenience does this cause? This should be easy to change back, I just wanted to gather a bit more input before doing this. |
Hmm... actually the fact that this requires two subtype pairs to trigger, means there may actually be some (unrelated) bug. |
If mypy would warn about redundant unions, this bug would actually be a feature ;) edit: would be a generalization of flake8-pyi's |
We are using this pattern in I find it confusing that |
I think it would just mean that we need to de-duplicate all unions that occur in |
My only concern here is that from a user perspective, I think it is valuable to have Another example is using For |
The original example in this issue is a particularly pernicious example: it removed |
It would be interesting to experiment with disabling complex union simplification. Duplicate entries should always be removed (e.g. Also, as long as we perform union simplification, I think that |
Yeah, the one downside however, is if we use |
What about not doing promotions during union simplification, i.e. |
I was thinking about this (and it will indeed fix this issue), but it may be a big change, we can of course try and see |
Btw not using promotions during union simplification is a generally right thing to do, since we ignore promotions when checking |
OK, so it is a bit more tricky than that. Previous attempt to ignore promotions in |
The |
Running mypy 0.982 on the example from the top of this issue still triggers an error:
|
The fix was intentionally not included in 0.982, since it required a too big change for a point release. It will be included in 0.990 |
@twoertwein You can try running mypy from master to double-check that your issue is fixed. |
It works with mypy 0.990+dev.dc5c299aa190949f2300b163ccc10257a779006d :) |
Bug Report
Code below works fine with 0.971, but fails with 0.981. It seems that 0.971 did type narrowing on contents of a
Union
, but 0.981 doesn't do it consistently.To Reproduce
Expected Behavior
Version 0.971 output:
Actual Behavior
Version 0.981 output:
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: