-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
sum
stub too permissive
#7574
Comments
It's easy to put a TypeVar bound on the first overload, but
Fixing this properly would require python/typing#1043. |
Even that's actually a little tricky, because you get into the question of what is a number. For example, this works fine, even though there's not really anything that these classes all have in common either from a nominal or structural point of view: >>> import numpy as np
>>> from fractions import Fraction
>>> sum((3, np.float64(), Fraction(7, 22), complex(8, 0), 9.83))
(21.14818181818182+0j) This does too: >>> from decimal import Decimal
>>> sum((3, Decimal('0.98')))
Decimal('3.98') But these all break: >>> from decimal import Decimal
>>> sum((2.34, Decimal('3.87')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal'
>>> from fractions import Fraction
>>> sum((Fraction(2, 3), Decimal('2.3')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Fraction' and 'decimal.Decimal'
>>> sum((complex(2, 0), Decimal('2.34')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'complex' and 'decimal.Decimal'
>>> import numpy as np
>>> sum((np.float64(), Decimal('2.34')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal' |
We could at least bound the TypeVar to a Protocol defining |
Currently the stub for the
sum
built-in allows any iterable as an argumenttypeshed/stdlib/builtins.pyi
Lines 1480 to 1489 in ae6ff79
This means mypy has no issue with:
Which results in this error at runtime:
context: I was introducing someone to mypy and used the example above as something that mypy should flag, but turns out it wasn't being flagged so logging here.
The text was updated successfully, but these errors were encountered: