Author: Armin Rigo <armin.r...@gmail.com> Branch: py3.6 Changeset: r94928:b72ee387ca84 Date: 2018-07-29 15:29 +0000 http://bitbucket.org/pypy/pypy/changeset/b72ee387ca84/
Log: Merged in jKorvin/pypy/fix-float-deprecation-warning (pull request #618) 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) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit