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
datetime argument handling inconsistent; should accept logical integers, not coercible values #65060
Comments
Per my comments on bpo-20858, datetime's argument handling is inconsistent. By using the 'i' format code, non-integer types are being coerced to int, even as other equivalent non-integer types are accepted (sometimes in a lossy fashion). Example: >>> from decimal import Decimal as d
>>> from datetime import datetime
>>> datetime(d("2000.5"), 1, 2)
datetime.datetime(2000, 1, 2, 0, 0)
>>> datetime(2000.0, 1, 2)
Traceback (most recent call last):
...
TypeError: integer argument expected, got float Note that the latter case would not lose data by coercion to int, yet it raises an error anyway, while the former is losing data silently. Similar discrepancies would occur between passing a str argument vs. a user-defined string type (the latter would need to implement __int__ to get the coercion that str gets through a special case in the int constructor, making int(x) and x.__int__() subtly different, since there is no str.__int__). For consistency, it seems like we should primarily be using typecode 'n', not 'i', so logical integers are properly converted, while anything else (including float/Decimal/str/user-string) must be cast by the caller to avoid the risk of silent data loss. I suspect tons of modules make similar "mistakes" in the interface (until today, I didn't realize PyLong_AsLong, and by extension, the 'i' type code would coerce non-integral types). I'm looking to start contributing to Python, and at least for time being I think I'll keep the bug targeted. I haven't tested, but I suspect this bug is in all versions of Python. Clearly it's not a bug or security issue worth a backport prior to 3.4 though. I'd happily create and submit a patch if this is a desired fix; I'd appreciate any pointers on how to get started (to date, all my Python C extension development has been for organization internal modules). |
You probably know that the relevant code is in http://hg.python.org/cpython/file/47f37a688c4c/Modules/_datetimemodule.c#l4059 The devguide should get you started: |
Thank you very much. Very helpful. I'll see about whipping up a preliminary patch. |
Could be related to some discussions in this PR: #14842 |
See also bpo-35707. |
This is essentially a duplicate of bpo-36048. The example is now deprecated: >>> from decimal import Decimal
>>> from datetime import datetime
>>> datetime(Decimal("2000.5"), 1, 2)
<stdin>:1: DeprecationWarning: an integer is required (got type decimal.Decimal). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
datetime.datetime(2000, 1, 2, 0, 0) So I think that this can be closed. |
This issue has been fixed in Python 3.3 by bpo-17576 which started to emit the DeprecationWarning. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: