Message274208
The different data types make different choices:
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> (1).__round__(None)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
(1).__round__(None)
TypeError: 'NoneType' object cannot be interpreted as an integer
>>> (1.0).__round__(None)
1
>>> Decimal(1).__round__(None)
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
Decimal(1).__round__(None)
TypeError: optional arg must be an integer
>>> Fraction(1, 1).__round__(None)
1
>>> from _pydecimal import Decimal
>>> Decimal(1).__round__(None)
1
For Fraction and _pydecimal, the behavior comes from using None as a placeholder (which is common and normal in pure python code). For float there is explicit code to test for the None case. For int, the None test was omitted (perhaps a mistake) and the error is raised by PyNumber_Index.
Looking through tests, only Lib/test/test_float.py tests for None being allowable. Elsewhere, it seems to be an implementation detail.
The old Python 2 version of the round() function never let None be passed in (because it used PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round"). That logic seems to get lost in the Python 3 version when __round__ was introduced.
To resolve the differences, I think the round() function should explicitly check for None and replace it with zero before calling the underling __round__ functions where we would allow variable according the needs of the implementation. |
|
Date |
User |
Action |
Args |
2016-09-02 08:13:46 | rhettinger | set | recipients:
+ rhettinger, Jonatan Skogsfors |
2016-09-02 08:13:46 | rhettinger | set | messageid: <1472804026.23.0.217669017032.issue27936@psf.upfronthosting.co.za> |
2016-09-02 08:13:46 | rhettinger | link | issue27936 messages |
2016-09-02 08:13:45 | rhettinger | create | |
|