[pypy-commit] pypy fix-float-deprecation-warning: Add deprecation warning if type of result of __float__ is float inherit class
Evgenii Gorinov
pypy.commits at gmail.com
Sun Jul 29 11:30:10 EDT 2018
Author: Evgenii Gorinov <jonny-k at yandex-team.ru>
Branch: fix-float-deprecation-warning
Changeset: r94927:817e823be049
Date: 2018-07-29 14:45 +0100
http://bitbucket.org/pypy/pypy/changeset/817e823be049/
Log: Add deprecation warning if type of result of __float__ is float
inherit class
diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -220,6 +220,15 @@
if space.lookup(w_value, "__float__") is not None:
w_obj = space.float(w_value)
if space.is_w(w_floattype, space.w_float):
+ w_obj_type = space.type(w_obj)
+ if not space.is_w(w_obj_type, space.w_float):
+ space.warn(space.newtext(
+ "%s.__float__ returned non-float (type %s). "
+ "The ability to return an instance of a strict subclass "
+ "of float is deprecated, and may be removed "
+ "in a future version of Python." %
+ (space.type(w_value).name, w_obj_type.name)),
+ space.w_DeprecationWarning)
return w_obj
value = space.float_w(w_obj)
elif space.isinstance_w(w_value, space.w_unicode):
diff --git a/pypy/objspace/std/test/test_floatobject.py b/pypy/objspace/std/test/test_floatobject.py
--- a/pypy/objspace/std/test/test_floatobject.py
+++ b/pypy/objspace/std/test/test_floatobject.py
@@ -299,6 +299,21 @@
return 42.
assert float(X()) == 42.
+ def test_float_conversion_deprecated_warning(self):
+ import warnings
+
+ class X(float):
+ def __float__(self):
+ return self
+ x = X(42)
+
+ with warnings.catch_warnings(record=True) as log:
+ warnings.simplefilter("always", DeprecationWarning)
+ converted_x = float(x)
+
+ assert converted_x == 42. # sanity check
+ assert len(log) == 1
+
def test_round(self):
import math
assert 1.0 == round(1.0)
More information about the pypy-commit
mailing list