-
-
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
Funny behaviour with Union[Decimal, int] #1442
Comments
I'm not sure. I can repro this with the latest mypy from the repo. |
@gvanrossum Is this a typeshed bug? Note that |
But I can't repro it without using a Union. |
@gvanrossum Exactly, because from typing import Union
class Dec:
def __add__(self, x: Union['Dec', int]) -> 'Dec': pass # This accepts another Dec.
def __radd__(self, x: int) -> 'Dec': pass # But this does NOT!
def foo(bar: Union[Dec, int]):
d = None # type: Dec
print(d + bar)
print(bar + d) # Error, since Dec.__radd__ wants `int` for its lhs, but `bar` is `Union[Dec, int]` |
But at runtime, `__radd__` would not be invoked with a Decimal argument --
so its declaration feels correct.
|
So then the general issue is binary operators, I guess. It seems Mypy sees this: x + y # where x's type is Union[A,B], and y's type is C and tries to look for either
This is mostly a guess, though... |
@kirbyfan64 I believe that you are right. The logic for checking binary operations should probably decompose union types early and process each union item independently (and finally combine the results, possibly resulting in another union type). |
Maybe #1264 is relevant? (That's
holding up the stubs for fractions.py in typeshed, for example.)
|
That may be relevant, but this may also be a separate issue/bug. |
Improve operator methods for dateutil.relativedelta stubs: * `__add__` operator method could return other types than `relativedelta` (`datetime.date` or `datetime.datetime`) * use specific types of operators args instead of Any * mypy currently does not handle `Union` in op methods (see python/mypy#2129, python/mypy#1442, python/mypy#1264 for details), so I've overloaded it directly
Improve operator methods for dateutil.relativedelta stubs: * `__add__` operator method could return other types than `relativedelta` (`datetime.date` or `datetime.datetime`) * use specific types of operators args instead of Any * mypy currently does not handle `Union` in op methods (see python/mypy#2129, python/mypy#1442, python/mypy#1264 for details), so I've overloaded it directly
FWIW this particular example works correctly on master (due to a recent operator overhaul by @Michael0x2a ). We have a separate issue for general union destructuring in operator checks. |
Just for reference, the main issue is #2128 |
Considering the following program:
Why does
mypy
complain in the second print()?I'm using mypy 0.3.1 on Python 3.5.1, Ubuntu 16.04.
The text was updated successfully, but these errors were encountered: