Skip to content

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Sep 1, 2019

Only __index__ should be used to make integer conversions lossless.

https://bugs.python.org/issue37999

Only __index__ should be used to make integer conversions lossless.
@serhiy-storchaka serhiy-storchaka changed the title [3.10] bpo-37999: No longer use __int__ in implicit integer conversions. bpo-37999: No longer use __int__ in implicit integer conversions. May 26, 2020
Lib/datetime.py Outdated
@@ -2539,7 +2508,7 @@ def _name_from_offset(delta):
# Clean up unused names
del (_DAYNAMES, _DAYS_BEFORE_MONTH, _DAYS_IN_MONTH, _DI100Y, _DI400Y,
_DI4Y, _EPOCH, _MAXORDINAL, _MONTHNAMES, _build_struct_time,
_check_date_fields, _check_int_field, _check_time_fields,
_check_date_fields, _index, _check_time_fields,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's an effort to keep things in alphabetical order here; maybe move _index to the right place with respect to that order?

@serhiy-storchaka serhiy-storchaka force-pushed the int-implicit-lossless branch from 656c19e to 49aa158 Compare May 26, 2020 09:38
Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; I'm happy to see this finally reach completion.

Not sure I like the OverflowError for large negative values for math.factorial: that seems like a case of the implementation details leaking into the API. EDIT: This has been changed

Just to be clear, this PR doesn't change the fact that PyNumber_Index can still return something that's an instance of a strict subclass of int. Is that correct?

@serhiy-storchaka
Copy link
Member Author

Just to be clear, this PR doesn't change the fact that PyNumber_Index can still return something that's an instance of a strict subclass of int. Is that correct?

Yes, it is correct.

chrisnovakovic added a commit to chrisnovakovic/cpython that referenced this pull request Mar 17, 2023
…ted as integers

Prior to pythongh-11952, several standard library functions that expected
integer arguments would nevertheless silently accept (and truncate)
non-integer arguments. This behaviour was deprecated in pythongh-11952, and
removed in pythongh-15636.

However, it may be possible to interpret some non-integer numeric types
(such as `decimal.Decimal`s) as integers if they contain no fractional
part. Implement `__index__` for `decimal.Decimal`, returning an integer
representation of the value if it does not contain a fractional part or
raising a `TypeError` if it does.
@bkeryan bkeryan mentioned this pull request Mar 26, 2025
1 task
bkeryan added a commit to bkeryan/hightime that referenced this pull request Mar 26, 2025
`datetime` used to accept objects that implement `__int__` or `__index__`, but in Python 3.10 it was changed to accept only `__index__`.
python/cpython#15636
tkrebes pushed a commit to ni/hightime that referenced this pull request Apr 2, 2025
* hightime: Update Python versions in tox.ini

* hightime: Update trove classifiers

* hightime: More tox.ini updates

* hightime: Use __index__ instead of __int__

`datetime` used to accept objects that implement `__int__` or `__index__`, but in Python 3.10 it was changed to accept only `__index__`.
python/cpython#15636

* github: Update Python versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants