Skip to content

bpo-44977: Deprecate delegation of int to __trunc__ #31031

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

Merged
Merged
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
3 changes: 3 additions & 0 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,9 @@ are always available. They are listed here in alphabetical order.
.. versionchanged:: 3.8
Falls back to :meth:`__index__` if :meth:`__int__` is not defined.

.. versionchanged:: 3.11
The delegation to :meth:`__trunc__` is deprecated.


.. function:: isinstance(object, classinfo)

Expand Down
3 changes: 3 additions & 0 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,9 @@ left undefined.
The built-in function :func:`int` falls back to :meth:`__trunc__` if neither
:meth:`__int__` nor :meth:`__index__` is defined.

.. versionchanged:: 3.11
The delegation of :func:`int` to :meth:`__trunc__` is deprecated.


.. _context-managers:

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ Deprecated
as deprecated, its docstring is now corrected).
(Contributed by Hugo van Kemenade in :issue:`45837`.)

* The delegation of :func:`int` to :meth:`__trunc__` is now deprecated. Calling
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The delegation of :func:`int` to :meth:`__trunc__` is now deprecated. Calling
* The delegation of :class:`int` to :meth:`__trunc__` is now deprecated. Calling

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int() is referred as a function here, not a class.

The current text is correct.

``int(a)`` when ``type(a)`` implements :meth:`__trunc__` but not
:meth:`__int__` or :meth:`__index__` now raises a :exc:`DeprecationWarning`.
(Contributed by Zackery Spytz in :issue:`44977`.)

* The following have been deprecated in :mod:`configparser` since Python 3.2.
Their deprecation warnings have now been updated to note they will removed in
Python 3.12:
Expand All @@ -468,6 +473,7 @@ Deprecated

(Contributed by Hugo van Kemenade in :issue:`45173`.)


Removed
=======

Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,6 @@ def format_impl(self, spec):
("__format__", format, format_impl, set(), {}),
("__floor__", math.floor, zero, set(), {}),
("__trunc__", math.trunc, zero, set(), {}),
("__trunc__", int, zero, set(), {}),
("__ceil__", math.ceil, zero, set(), {}),
("__dir__", dir, empty_seq, set(), {}),
("__round__", round, zero, set(), {}),
Expand Down
27 changes: 18 additions & 9 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,14 @@ def __trunc__(self):
class JustTrunc(base):
def __trunc__(self):
return 42
self.assertEqual(int(JustTrunc()), 42)
with self.assertWarns(DeprecationWarning):
self.assertEqual(int(JustTrunc()), 42)

class ExceptionalTrunc(base):
def __trunc__(self):
1 / 0
with self.assertRaises(ZeroDivisionError):
with self.assertRaises(ZeroDivisionError), \
self.assertWarns(DeprecationWarning):
int(ExceptionalTrunc())

for trunc_result_base in (object, Classic):
Expand All @@ -385,7 +387,8 @@ def __index__(self):
class TruncReturnsNonInt(base):
def __trunc__(self):
return Index()
self.assertEqual(int(TruncReturnsNonInt()), 42)
with self.assertWarns(DeprecationWarning):
self.assertEqual(int(TruncReturnsNonInt()), 42)

class Intable(trunc_result_base):
def __int__(self):
Expand All @@ -394,7 +397,8 @@ def __int__(self):
class TruncReturnsNonIndex(base):
def __trunc__(self):
return Intable()
self.assertEqual(int(TruncReturnsNonInt()), 42)
with self.assertWarns(DeprecationWarning):
self.assertEqual(int(TruncReturnsNonInt()), 42)

class NonIntegral(trunc_result_base):
def __trunc__(self):
Expand All @@ -405,7 +409,8 @@ class TruncReturnsNonIntegral(base):
def __trunc__(self):
return NonIntegral()
try:
int(TruncReturnsNonIntegral())
with self.assertWarns(DeprecationWarning):
int(TruncReturnsNonIntegral())
except TypeError as e:
self.assertEqual(str(e),
"__trunc__ returned non-Integral"
Expand All @@ -423,7 +428,8 @@ class TruncReturnsBadInt(base):
def __trunc__(self):
return BadInt()

with self.assertRaises(TypeError):
with self.assertRaises(TypeError), \
self.assertWarns(DeprecationWarning):
int(TruncReturnsBadInt())

def test_int_subclass_with_index(self):
Expand Down Expand Up @@ -517,13 +523,16 @@ def __trunc__(self):
self.assertIs(type(n), int)

bad_int = TruncReturnsBadInt()
self.assertRaises(TypeError, int, bad_int)
with self.assertWarns(DeprecationWarning):
self.assertRaises(TypeError, int, bad_int)

good_int = TruncReturnsIntSubclass()
n = int(good_int)
with self.assertWarns(DeprecationWarning):
n = int(good_int)
self.assertEqual(n, 1)
self.assertIs(type(n), int)
n = IntSubclass(good_int)
with self.assertWarns(DeprecationWarning):
n = IntSubclass(good_int)
self.assertEqual(n, 1)
self.assertIs(type(n), IntSubclass)

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ def __long__(self):
return 42
def __trunc__(self):
return 1729
self.assertEqual(int(LongTrunc()), 1729)
with self.assertWarns(DeprecationWarning):
self.assertEqual(int(LongTrunc()), 1729)

def check_float_conversion(self, n):
# Check that int -> float conversion behaviour matches
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The delegation of :func:`int` to :meth:`__trunc__` is now deprecated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The delegation of :func:`int` to :meth:`__trunc__` is now deprecated.
The delegation of :class:`int` to :meth:`__trunc__` is now deprecated.

Calling ``int(a)`` when ``type(a)`` implements :meth:`__trunc__` but not
:meth:`__int__` or :meth:`__index__` now raises a :exc:`DeprecationWarning`.
5 changes: 5 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,11 @@ PyNumber_Long(PyObject *o)
}
trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
if (trunc_func) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"The delegation of int() to __trunc__ is deprecated.", 1)) {
Py_DECREF(trunc_func);
return NULL;
}
result = _PyObject_CallNoArgs(trunc_func);
Py_DECREF(trunc_func);
if (result == NULL || PyLong_CheckExact(result)) {
Expand Down