-
-
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
Mypy fails on Union[float, int] + Union[float, int]
#2128
Comments
That's definitely problematic. I suspect it's because we don't try all combinations of all unions involved in an expression. We should probably do better, although overuse of this could cause performance regressions. |
Now I believe that this is another incarnation of overload python/typing#253 and therefore #1941 and the others mentioned there. I cannot base this claim though. |
Regarding @gvanrossum's comment: "overuse of this could cause performance regressions"… Uh… code that's wrong isn't especially useful no matter how fast it is. If it's too slow for whatever criteria, then it would be good to make this case a warning rather than an error, as false positives break CI builds that are trying to use |
(or give up with a warning if the matrix is too large) |
I've encountered this bug most recently in version 0.610. A fix would be much appreciated! |
Here is my diagnosis, mypy first asks "What is the signature of Off-topic: it is sad to see many issues that have straightforward fixes, but we just don't have time to implement them, sigh. |
I have also just encountered this bug (or rather, an equivalent bug concerning |
Assigning myself -- I'm planning on making some fixes to operators in the near future, so I might as well look at this one too. |
This pull request resolves python#2128 -- it modifies how we check operators to add support for operations like `Union[int, float] + Union[int, float]`. This approach basically iterates over all possible variations of the left and right operands when they're unions and uses the union of the resulting inferred type as the type of the overall expression. Some implementation notes: 1. I attempting "destructuring" just the left operand, which is basically the approach proposed here: python#2128 (comment) Unfortunately, I discovered it became necessary to also destructure the right operand to handle certain edge cases -- see the testOperatorDoubleUnionInterwovenUnionAdd test case. 2. This algorithm varies slightly from what we do for union math in that we don't attempt to "preserve" the union/we always destructure both operands. I'm fairly confident that this is type-safe; I plan on testing this pull request against some internal code bases to help us gain more confidence.
* Add support for operators with union operands This pull request resolves #2128 -- it modifies how we check operators to add support for operations like `Union[int, float] + Union[int, float]`. This approach basically iterates over all possible variations of the left and right operands when they're unions and uses the union of the resulting inferred type as the type of the overall expression. Some implementation notes: 1. I attempting "destructuring" just the left operand, which is basically the approach proposed here: #2128 (comment) Unfortunately, I discovered it became necessary to also destructure the right operand to handle certain edge cases -- see the testOperatorDoubleUnionInterwovenUnionAdd test case. 2. This algorithm varies slightly from what we do for union math in that we don't attempt to "preserve" the union/we always destructure both operands.
Results in:
Interestingly both
and
work fine
The text was updated successfully, but these errors were encountered: