Author serhiy.storchaka
Recipients mark.dickinson, serhiy.storchaka
Date 2016-05-09.13:46:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1462801593.23.0.789372695786.issue26983@psf.upfronthosting.co.za>
In-reply-to
Content
The float constructor can return an instance of float subclass.

>>> class FloatSubclass(float):
...     pass
... 
>>> class BadFloat:
...     def __float__(self):
...         return FloatSubclass(1.2)
... 
>>> type(float(BadFloat()))
<class '__main__.FloatSubclass'>

Comparing with other types, complex() always returns complex:

>>> class ComplexSubclass(complex):
...     pass
... 
>>> class BadComplex:
...     def __complex__(self):
...         return ComplexSubclass(1.2, 3.4)
... 
>>> type(complex(BadComplex()))
<class 'complex'>

And int() can return an instance of int subclass, but this behavior is deprecated:

>>> 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

May be we should either deprecate __float__ returning non-float (as for int), or convert the result to exact float (as for complex).

The constructor of float subclass always returns an instance of correct type.

>>> class FloatSubclass2(float):
...     pass
... 
>>> type(FloatSubclass2(BadFloat()))
<class '__main__.FloatSubclass2'>
History
Date User Action Args
2016-05-09 13:46:33serhiy.storchakasetrecipients: + serhiy.storchaka, mark.dickinson
2016-05-09 13:46:33serhiy.storchakasetmessageid: <1462801593.23.0.789372695786.issue26983@psf.upfronthosting.co.za>
2016-05-09 13:46:32serhiy.storchakalinkissue26983 messages
2016-05-09 13:46:32serhiy.storchakacreate