-
-
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
Inconsistency with unions and overloads in when checking overlapping op methods #2129
Comments
There's also #1264, and and perhaps #1987. Jukka has said a few times that On Wed, Sep 14, 2016 at 12:19 AM, Michael Lee [email protected]
--Guido van Rossum (python.org/~guido) |
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
I tried re-running all three of the above examples on master, and they all seem to typecheck now... I don't think it was due to any of my overload changes though -- I suspect this was fixed a year ago by #3086? (Also, apparently I was the one who originally submitted this issue? I completely forgot about this...) |
As a part of my adventures in attempting to add semi-usable stubs for the fractions and statistics modules, I ran into an edge case involving the
__add__
and__radd__
methods (or any pair of operator methods). Here is a simplified version ofdecimal.Decimal
:When typechecking this, I got the following (and somewhat cryptic) error message:
It seems mypy wanted me to do this instead:
However, the following code which uses overrides instead of Union does not result in any errors, which feels inconsistent:
After some digging, it appeared to me that the reason for this behavior is because the
check_overlapping_op_methods
withinchecker.py
handles the case whereforward_type
is either aCallableType
,Overloaded
, orAnyType
, but not the case where it's aUnionType
.Is this intentional, or an oversight? I think this is a bug, mainly since if mypy accepts version 3, it ought to accept verison 1? Or perhaps the fact that
__radd__
should never need to accept a value of the same type as its instance means that the version 2 is more correct and versions 1 and 3 ought to be rejected?(For what it's worth, adding a new case to make
UnionType
do the same thing as theOverloaded
case makes version 1 pass.)This is perhaps related: #1442
The text was updated successfully, but these errors were encountered: