Author rhettinger
Recipients Jonatan Skogsfors, rhettinger
Date 2016-09-02.08:13:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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>
TypeError: 'NoneType' object cannot be interpreted as an integer
>>> (1.0).__round__(None)
>>> Decimal(1).__round__(None)
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
TypeError: optional arg must be an integer
>>> Fraction(1, 1).__round__(None)
>>> from _pydecimal import Decimal
>>> Decimal(1).__round__(None)

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/ 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:46rhettingersetrecipients: + rhettinger, Jonatan Skogsfors
2016-09-02 08:13:46rhettingersetmessageid: <>
2016-09-02 08:13:46rhettingerlinkissue27936 messages
2016-09-02 08:13:45rhettingercreate