[proxy] bugs.python.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

Message 265196 - Python tracker

Message265196

Author serhiy.storchaka
Recipients mark.dickinson, serhiy.storchaka
Date 2016-05-09.14:02:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1462802551.63.0.282711121635.issue26984@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-05-09 14:02:31serhiy.storchakasetrecipients: + serhiy.storchaka, mark.dickinson
2016-05-09 14:02:31serhiy.storchakasetmessageid: <1462802551.63.0.282711121635.issue26984@psf.upfronthosting.co.za>
2016-05-09 14:02:31serhiy.storchakalinkissue26984 messages
2016-05-09 14:02:31serhiy.storchakacreate