Issue 26984: int() can return not exact int instance (original) (raw)

Created on 2016-05-09 14:02 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
int_exact.patch serhiy.storchaka,2016-07-01 12:23 review
Messages (7)
msg265196 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-09 14:02
The int constructor can return an instance of int subclass. >>> class BadTrunc: ... def __trunc__(self): ... return True ... >>> int(BadTrunc()) True When __int__ returns non-exact int, at least a warning is emitted: >>> class BadInt: ... def __int__(self): ... return True ... >>> int(BadInt()) __main__:1: DeprecationWarning: __int__ returned non-int (type bool). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python. True The constructor of int subclass always return an instance of correct type: >>> class IntSubclass(int): ... pass ... >>> type(IntSubclass(BadTrunc())) <class '__main__.IntSubclass'> >>> type(IntSubclass(BadInt())) __main__:1: DeprecationWarning: __int__ returned non-int (type bool). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python. <class '__main__.IntSubclass'> I don't know if it is worth to deprecate __trunc__ returning non-exact int, since this special method is used in math.trunc(). But I think that the int constructor should convert its result to exact int. If some preparatory period is needed, it can first start to emit FutureWarning.
msg265210 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-05-09 16:43
> But I think that the int constructor should convert its result to exact int. Agreed. See also issue 17576 and previous discussions on python-dev (thread started in March, but most of the messages in April): - https://mail.python.org/pipermail/python-dev/2013-March/125022.html - https://mail.python.org/pipermail/python-dev/2013-April/125042.html I don't have strong feelings about `__trunc__` (or `__floor__` and `__ceil__`); it seems fine to me for those to be returning something other than a strict int.
msg269663 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-01 12:23
Proposed patch makes int() always returning exact int.
msg270626 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-07-17 10:04
Could you please make a review Mark?
msg273057 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-08-18 18:55
> Could you please make a review Mark? Sorry, Serhiy. I missed this. I've got some time off coming up, so I plan to look at this in the next few days.
msg273291 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-08-21 10:40
The patch LGTM. I'm still in two minds about whether `__trunc__` should be required to return a strict `int`, but for now it's probably better to go with the status quo.
msg273305 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-21 17:03
New changeset 81f229262921 by Serhiy Storchaka in branch 'default': Issue #26984: int() now always returns an instance of exact int. https://hg.python.org/cpython/rev/81f229262921
History
Date User Action Args
2022-04-11 14:58:30 admin set github: 71171
2016-08-21 17:17:05 serhiy.storchaka set status: open -> closedresolution: fixedstage: commit review -> resolved
2016-08-21 17:03:33 python-dev set nosy: + python-devmessages: +
2016-08-21 10:41:31 mark.dickinson set assignee: mark.dickinson -> serhiy.storchakastage: patch review -> commit review
2016-08-21 10:40:56 mark.dickinson set messages: +
2016-08-18 18:55:03 mark.dickinson set messages: +
2016-08-18 11:52:43 mark.dickinson set assignee: mark.dickinson
2016-07-17 10:04:38 serhiy.storchaka set messages: +
2016-07-01 12:23:12 serhiy.storchaka set files: + int_exact.patchkeywords: + patchmessages: + stage: patch review
2016-05-09 16:43:36 mark.dickinson set messages: +
2016-05-09 14:02:31 serhiy.storchaka create