-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
abs(Union[int, Decimal]) is inferred as object #8601
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
Comments
For context, the definition of def abs(__n: SupportsAbs[_T]) -> _T: ... Considering the definition of I feel like mypy should be able to use these stubs to infer that |
Yeah, I agree that would probably be a better type in this case. But whether to infer a union type or a join is tricky and changing behavior will break code in places; not sure if this one has a fix that would be better. Fix for your particular issue might be to write a wrapper with a better type. |
I ended up just casting |
I think the following is a related example:
This gives the error on the last line:
EDIT: to be clear, there is supposed to be an error, but I feel mypy should infer the type |
It seems to me that python/typeshed#5275 is related too. There, @erictraut describes:
Are there any plans to fix this and change the way the constraint solver works? PS: This is the first issue that I dug up trough the search that seems to be related and I am new to this project. As was said here, there are probably related issues. Feel free to refer me to a more relevant issue/feature if this has been discussed. |
Run into this bug today too. This snippet: from decimal import Decimal
m = min(Decimal(1), 2)
Decimal(m) produces:
using I've also read python/typeshed#5275 and it looks that this is fine with Pyright. Relevant quotes from that issue seem to be:
and
|
I'm afraid I'm not sure whether I'm reporting a bug or requesting a feature. I'm happy to leave that up for you all to triage. I'll start with what I'm seeing and what I'd like to see, and then get a little bit into the why.
Here's a minimal reproduction:
With Python 3.7.3 (from Debian buster) and mypy 0.770 (from PyPI), checking this code returns an error:
The return type of
abs
is inferred as object, I think because that's the common supertype of int and Decimal. It would be nice if, one way or another, the return type ofabs
could be inferred as some higher numeric type.The context here is I'm working on accounting software where I want to be careful to do decimal math throughout. In other words, I never want to deal with the decimal.FloatOperation signal. For functions that do basic arithmetic or comparisons across numbers, it's fine to accept arguments that are either int or Decimal, and it's convenient for callers if I can annotate that argument type rather than requiring them to convert their integer arguments to Decimal all the time.
The text was updated successfully, but these errors were encountered: