-
-
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
"Unsupported operand types" error with dictionary that has some Decimal or Fraction values #4548
Comments
More about this bug:
|
This is because mypy tries to infer a common type for the dictionary, and because the only common type for int and Decimal that it knows about is |
This seems strange. How is this behavior expected? Who is it expected by? As far as I know, all ints can be converted to Decimals, so shouldn't the type of the dictionary be If not, then shouldn't there be some common type that would let a static type checker determine that it is valid to add any of the values to an int? Like maybe |
I thought Decimal follows the |
Mypy is working as designed, i.e. this is not a bug but a "feature". However, we may change this behavior in the future (#3816).
Whether something can be converted to something else plays little role when inferring types in mypy. An
There are multiple potential common types, and mypy can't make the decision reliably. Maybe somebody would expect |
Closing as essentially a duplicate of #3816. Feel free to continue the discussion there. |
Finally, sometimes one can define a custom protocol, to list exactly what should be supported/provided by values in the |
I am using Python 3.6.4. I have a file with these 2 lines:
If I run it through mypy, I get this error:
supports_int_test.py:2: error: Unsupported operand types for + ("int" and "object")
This is strange. I would think that mypy should be able to figure out that regardless of which value is picked from the dictionary, it should be something that can be added to 1. The expression
1 + {'b': Decimal('1.5')}['b']
does not produce any errors.If I use an int for the
'b'
value (making all of the values ints), I don't get any errors. If I use a float for the'b'
value, I also don't get any errors. But if I use a Fraction, I get a similar error.The expression
1 + {'b': Decimal('1.5')}['c']
(which deliberately tries to access a nonexistent key) doesn't give me any errors but1 + {'a': 1, 'b': Decimal('1.5')}['c']
gives me a similar error.This is happening with both mypy 0.560 and with the latest version from master (mypy 0.570-dev-26b51e554d7426a0aed6ad55bae7b10a4619c093).
The text was updated successfully, but these errors were encountered: