[New-bugs-announce] [issue26984] int() can return not exact int instance
Serhiy Storchaka
report at bugs.python.org
Mon May 9 10:02:31 EDT 2016
New submission from Serhiy Storchaka:
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.
----------
components: Interpreter Core
messages: 265196
nosy: mark.dickinson, serhiy.storchaka
priority: normal
severity: normal
status: open
title: int() can return not exact int instance
type: behavior
versions: Python 3.6
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26984>
_______________________________________
More information about the New-bugs-announce
mailing list