[issue26983] float() can return not exact float instance

Mark Dickinson report at bugs.python.org
Wed May 11 04:05:24 EDT 2016


Mark Dickinson added the comment:

+1 for the idea, and the patch LGTM.

I was initially a bit confused by the wording of the warning: presumably, we're not going to change Python to make returning an instance of a strict float subclass from __float__ illegal (I don't really see how that would be possible); we're just going to check the return type at the internal call sites to __float__ and raise if we get something other than an exact float. That is, I'd still expect this code to work on future versions of Python:

>>> class MyFloat(float): pass
... 
>>> class A:
...     def __float__(self): return MyFloat()
... 
>>> a = A()
>>> a.__float__()
0.0

But these would both become an error in Python 3.7 (say):

>>> float(a)
0.0
>>> math.sqrt(a)
0.0

Does that match your thinking?

We should probably add issues for checking and fixing other places that __float__ is used internally (like the math module).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26983>
_______________________________________


More information about the Python-bugs-list mailing list