Skip to content

gh-109311: Remove support for non-complex/float types in __complex/float__ #112680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ wave
(Contributed by Bénédikt Tran in :gh:`133873`.)


Others
------

* Removed support for handling returned
non-:class:`float`/:class:`complex` types from
:meth:`~object.__float__` and :meth:`~object.__complex__`,
respectively. This had previously raised a
:exc:`DeprecationWarning` since Python 3.7.
(Contributed by Sergey B Kirpichev in :gh:`109311`.)


Porting to Python 3.15
======================

Expand Down
27 changes: 6 additions & 21 deletions Lib/test/test_capi/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,13 @@ def test_realasdouble(self):
# Test types with __complex__ dunder method
self.assertEqual(realasdouble(Complex()), 4.25)
self.assertRaises(TypeError, realasdouble, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadComplex2()), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, realasdouble, BadComplex2())
self.assertRaises(TypeError, realasdouble, BadComplex2())
self.assertRaises(RuntimeError, realasdouble, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(realasdouble(Float()), 4.25)
self.assertRaises(TypeError, realasdouble, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(realasdouble(BadFloat2()), 4.25)
self.assertRaises(TypeError, realasdouble, BadFloat2())

self.assertRaises(TypeError, realasdouble, object())

Expand All @@ -115,18 +110,13 @@ def test_imagasdouble(self):
# Test types with __complex__ dunder method
self.assertEqual(imagasdouble(Complex()), 0.5)
self.assertRaises(TypeError, imagasdouble, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadComplex2()), 0.5)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, imagasdouble, BadComplex2())
self.assertRaises(TypeError, imagasdouble, BadComplex2())
self.assertRaises(RuntimeError, imagasdouble, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(imagasdouble(Float()), 0.0)
self.assertRaises(TypeError, imagasdouble, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(imagasdouble(BadFloat2()), 0.0)
self.assertRaises(TypeError, imagasdouble, BadFloat2())

self.assertRaises(TypeError, imagasdouble, object())

Expand All @@ -150,18 +140,13 @@ def test_asccomplex(self):
# Test types with __complex__ dunder method
self.assertEqual(asccomplex(Complex()), 4.25+0.5j)
self.assertRaises(TypeError, asccomplex, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadComplex2()), 4.25+0.5j)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asccomplex, BadComplex2())
self.assertRaises(TypeError, asccomplex, BadComplex2())
self.assertRaises(RuntimeError, asccomplex, BadComplex3())

# Test types with __float__ dunder method
self.assertEqual(asccomplex(Float()), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asccomplex(BadFloat2()), 4.25+0.0j)
self.assertRaises(TypeError, asccomplex, BadFloat2())

self.assertRaises(TypeError, asccomplex, object())

Expand Down
6 changes: 1 addition & 5 deletions Lib/test/test_capi/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ def __float__(self):
self.assertRaises(RuntimeError, asdouble, BadFloat3())
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadIndex2()), 1.)
with self.assertWarns(DeprecationWarning):
self.assertEqual(asdouble(BadFloat2()), 4.25)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, asdouble, BadFloat2())
self.assertRaises(TypeError, asdouble, BadFloat2())
self.assertRaises(TypeError, asdouble, object())
self.assertRaises(TypeError, asdouble, NULL)

Expand Down
9 changes: 3 additions & 6 deletions Lib/test/test_capi/test_getargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,7 @@ def test_f(self):
self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5)
self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5)
self.assertRaises(TypeError, getargs_f, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_f(BadFloat2()), 4.25)
self.assertRaises(TypeError, getargs_f, BadFloat2())
self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5)
self.assertEqual(getargs_f(Index()), 99.0)
self.assertRaises(TypeError, getargs_f, Int())
Expand Down Expand Up @@ -485,8 +484,7 @@ def test_d(self):
self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5)
self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5)
self.assertRaises(TypeError, getargs_d, BadFloat())
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_d(BadFloat2()), 4.25)
self.assertRaises(TypeError, getargs_d, BadFloat2())
self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5)
self.assertEqual(getargs_d(Index()), 99.0)
self.assertRaises(TypeError, getargs_d, Int())
Expand All @@ -509,8 +507,7 @@ def test_D(self):
self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j)
self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j)
self.assertRaises(TypeError, getargs_D, BadComplex())
with self.assertWarns(DeprecationWarning):
self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j)
self.assertRaises(TypeError, getargs_D, BadComplex2())
self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j)
self.assertEqual(getargs_D(Index()), 99.0+0j)
self.assertRaises(TypeError, getargs_D, Int())
Expand Down
6 changes: 1 addition & 5 deletions Lib/test/test_capi/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,7 @@ def test_float(self):
self.assertEqual(float_(IndexLike.with_val(-1)), -1.0)

self.assertRaises(TypeError, float_, FloatLike.with_val(687))
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, float_, FloatLike.with_val(subclassof(float)(4.25)))
with self.assertWarns(DeprecationWarning):
self.assertEqual(float_(FloatLike.with_val(subclassof(float)(4.25))), 4.25)
self.assertRaises(TypeError, float_, FloatLike.with_val(subclassof(float)(4.25)))
self.assertRaises(RuntimeError, float_, FloatLike.with_exc(RuntimeError))

self.assertRaises(TypeError, float_, IndexLike.with_val(1.25))
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ def __complex__(self):
return None

check(complex(complex0(1j)), 0.0, 42.0)
with self.assertWarns(DeprecationWarning):
check(complex(complex1(1j)), 0.0, 2.0)
self.assertRaises(TypeError, complex, complex1(1j))
self.assertRaises(TypeError, complex, complex2(1j))

def test___complex__(self):
Expand Down
13 changes: 3 additions & 10 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,16 @@ def __float__(self):

self.assertEqual(float(FloatLike(42.)), 42.)
self.assertEqual(float(Foo2()), 42.)
with self.assertWarns(DeprecationWarning):
self.assertEqual(float(Foo3(21)), 42.)
self.assertRaises(TypeError, float, Foo3(21))
self.assertRaises(TypeError, float, Foo4(42))
self.assertEqual(float(FooStr('8')), 9.)

self.assertRaises(TypeError, time.sleep, FloatLike(""))

# Issue #24731
f = FloatLike(OtherFloatSubclass(42.))
with self.assertWarns(DeprecationWarning):
self.assertEqual(float(f), 42.)
with self.assertWarns(DeprecationWarning):
self.assertIs(type(float(f)), float)
with self.assertWarns(DeprecationWarning):
self.assertEqual(FloatSubclass(f), 42.)
with self.assertWarns(DeprecationWarning):
self.assertIs(type(FloatSubclass(f)), FloatSubclass)
self.assertRaises(TypeError, float, f)
self.assertRaises(TypeError, FloatSubclass, f)

self.assertEqual(float(MyIndex(42)), 42.0)
self.assertRaises(OverflowError, float, MyIndex(2**2000))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove support for returning non-:class:`complex` or non-:class:`float`
objects in :meth:`~object.__complex__` and :meth:`~object.__float__`.
22 changes: 4 additions & 18 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1607,25 +1607,11 @@ PyNumber_Float(PyObject *o)
return res;
}

if (!PyFloat_Check(res)) {
PyErr_Format(PyExc_TypeError,
"%.50s.__float__ returned non-float (type %.50s)",
Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name);
Py_DECREF(res);
return NULL;
}
/* Issue #26983: warn if 'res' not of exact type float. */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%.50s.__float__ returned non-float (type %.50s). "
"The ability to return an instance of a strict subclass of float "
"is deprecated, and may be removed in a future version of Python.",
Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name)) {
Py_DECREF(res);
return NULL;
}
double val = PyFloat_AS_DOUBLE(res);
PyErr_Format(PyExc_TypeError,
"%T.__float__ returned non-float (type %T)",
o, res);
Py_DECREF(res);
return PyFloat_FromDouble(val);
return NULL;
}

if (m && m->nb_index) {
Expand Down
22 changes: 5 additions & 17 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,23 +497,11 @@ try_complex_special_method(PyObject *op)
if (!res || PyComplex_CheckExact(res)) {
return res;
}
if (!PyComplex_Check(res)) {
PyErr_Format(PyExc_TypeError,
"__complex__ returned non-complex (type %.200s)",
Py_TYPE(res)->tp_name);
Py_DECREF(res);
return NULL;
}
/* Issue #29894: warn if 'res' not of exact type complex. */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"__complex__ returned non-complex (type %.200s). "
"The ability to return an instance of a strict subclass of complex "
"is deprecated, and may be removed in a future version of Python.",
Py_TYPE(res)->tp_name)) {
Py_DECREF(res);
return NULL;
}
return res;
PyErr_Format(PyExc_TypeError,
"%T.__complex__ returned non-complex (type %T)",
op, res);
Py_DECREF(res);
return NULL;
}
return NULL;
}
Expand Down
20 changes: 5 additions & 15 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,11 @@ PyFloat_AsDouble(PyObject *op)
return -1;
}
if (!PyFloat_CheckExact(res)) {
if (!PyFloat_Check(res)) {
PyErr_Format(PyExc_TypeError,
"%.50s.__float__ returned non-float (type %.50s)",
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name);
Py_DECREF(res);
return -1;
}
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%.50s.__float__ returned non-float (type %.50s). "
"The ability to return an instance of a strict subclass of float "
"is deprecated, and may be removed in a future version of Python.",
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name)) {
Py_DECREF(res);
return -1;
}
PyErr_Format(PyExc_TypeError,
"%T.__float__ returned non-float (type %T)",
op, res);
Py_DECREF(res);
return -1;
}

val = PyFloat_AS_DOUBLE(res);
Expand Down
Loading