Message265196
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. |
|
| Date |
User |
Action |
Args |
| 2016-05-09 14:02:31 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, mark.dickinson |
| 2016-05-09 14:02:31 | serhiy.storchaka | set | messageid: <[email protected]> |
| 2016-05-09 14:02:31 | serhiy.storchaka | link | issue26984 messages |
| 2016-05-09 14:02:31 | serhiy.storchaka | create | |
|