Description
Feature or enhancement
It should be possible for decimal.Decimal
s (and potentially other non-integer numeric types) to be interpreted as integers if doing so would not cause a loss of precision.
Pitch
As of GH-15636 (part of Python from 3.10.0 onwards), it's no longer possible to use certain numeric types as arguments to standard library functions when an integer is expected due to the removal of type coercion logic. This is, of course, quite sensible: datetime.datetime(year=2023, month=3, day=17.5)
is meaningless, and should result in a TypeError
rather than a datetime
object representing 2023-03-17 00:00:00. However, there are situations where certain numeric types might be coercible to integers - one example is a decimal.Decimal
with no fractional part. In these situations, I think it makes sense for the standard library functions whose behaviour changed following GH-15636 to behave as they did before.
In the Decimal
case, this would involve implementing __index__
in a way that returns an integer representation of the value if the number has no fractional part, or raising a TypeError
if it does. I believe this complies with the general understanding of how __index__
is supposed to work - from the What's New entry discussing PEP 357:
The return value must be either a Python integer or long integer. The interpreter will check that the type returned is correct, and raises a
TypeError
if this requirement isn't met.
I've implemented this in both _decimal
and _pydecimal
, with the following results:
>>> from decimal import Decimal
>>> from datetime import datetime
>>> datetime(year=Decimal(2023), month=Decimal(3), day=Decimal(17))
datetime.datetime(2023, 3, 17, 0, 0)
>>> datetime(year=Decimal(2023), month=Decimal(3), day=Decimal(17.5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot convert Decimal with fractional part to integer
I have a particular need for this to work with Decimal
, but perhaps the same could be done for other types in the standard library if there's enough interest.
Previous discussion
In issue36048, Serhiy discusses the rationale for this behaviour being deprecated in the first place (in Python 3.8) - the comment identifies the problem with "dropping the fractional part if the object is not [an] integral number", but doesn't address the corollary of that (i.e. permitting type coercion if the object is an integral number), which is the purpose of this issue.