classification
Title: int() can return not exact int instance
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: mark.dickinson, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-05-09 14:02 by serhiy.storchaka, last changed 2016-08-21 17:17 by serhiy.storchaka. 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
2016-08-21 17:17:05serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2016-08-21 17:03:33python-devsetnosy: + python-dev
messages: + msg273305
2016-08-21 10:41:31mark.dickinsonsetassignee: mark.dickinson -> serhiy.storchaka
stage: patch review -> commit review
2016-08-21 10:40:56mark.dickinsonsetmessages: + msg273291
2016-08-18 18:55:03mark.dickinsonsetmessages: + msg273057
2016-08-18 11:52:43mark.dickinsonsetassignee: mark.dickinson
2016-07-17 10:04:38serhiy.storchakasetmessages: + msg270626
2016-07-01 12:23:12serhiy.storchakasetfiles: + int_exact.patch
keywords: + patch
messages: + msg269663

stage: patch review
2016-05-09 16:43:36mark.dickinsonsetmessages: + msg265210
2016-05-09 14:02:31serhiy.storchakacreate