Skip to content

Commit 3a62c8f

Browse files
authored
gh-149537: Remove kw parameters from python version of reduce (#149538)
1 parent ebf6d9c commit 3a62c8f

4 files changed

Lines changed: 22 additions & 35 deletions

File tree

Doc/whatsnew/3.16.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ annotationlib
114114
Use :meth:`annotationlib.ForwardRef.evaluate`
115115
or :func:`typing.evaluate_forward_ref` instead.
116116

117+
functools
118+
---------
119+
120+
* Calling the Python implementation of :func:`functools.reduce` with *function*
121+
or *sequence* as keyword arguments has been deprecated since Python 3.14.
122+
117123
sysconfig
118124
---------
119125

Lib/functools.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def __ge__(self, other):
234234

235235
_initial_missing = object()
236236

237-
def reduce(function, sequence, initial=_initial_missing):
237+
def reduce(function, sequence, /, initial=_initial_missing):
238238
"""
239239
reduce(function, iterable, /[, initial]) -> value
240240
@@ -264,6 +264,11 @@ def reduce(function, sequence, initial=_initial_missing):
264264

265265
return value
266266

267+
try:
268+
from _functools import reduce
269+
except ImportError:
270+
pass
271+
267272

268273
################################################################################
269274
### partial() argument application
@@ -1178,31 +1183,3 @@ def __get__(self, instance, owner=None):
11781183
return val
11791184

11801185
__class_getitem__ = classmethod(GenericAlias)
1181-
1182-
def _warn_python_reduce_kwargs(py_reduce):
1183-
@wraps(py_reduce)
1184-
def wrapper(*args, **kwargs):
1185-
if 'function' in kwargs or 'sequence' in kwargs:
1186-
import os
1187-
import warnings
1188-
warnings.warn(
1189-
'Calling functools.reduce with keyword arguments '
1190-
'"function" or "sequence" '
1191-
'is deprecated in Python 3.14 and will be '
1192-
'forbidden in Python 3.16.',
1193-
DeprecationWarning,
1194-
skip_file_prefixes=(os.path.dirname(__file__),))
1195-
return py_reduce(*args, **kwargs)
1196-
return wrapper
1197-
1198-
reduce = _warn_python_reduce_kwargs(reduce)
1199-
del _warn_python_reduce_kwargs
1200-
1201-
# The import of the C accelerated version of reduce() has been moved
1202-
# here due to gh-121676. In Python 3.16, _warn_python_reduce_kwargs()
1203-
# should be removed and the import block should be moved back right
1204-
# after the definition of reduce().
1205-
try:
1206-
from _functools import reduce
1207-
except ImportError:
1208-
pass

Lib/test/test_functools.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,14 @@ def add(x, y):
11341134
self.assertRaises(TypeError, self.reduce, add, [0, 1], initial="")
11351135
self.assertEqual(self.reduce(42, "", initial="1"), "1") # func is never called with one item
11361136

1137+
def test_reduce_with_kwargs(self):
1138+
with self.assertRaises(TypeError):
1139+
self.reduce(function=lambda x, y: (x or 1) + y, sequence=[1, 2, 3, 4, 5])
1140+
with self.assertRaises(TypeError):
1141+
self.reduce(function=lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
1142+
with self.assertRaises(TypeError):
1143+
self.reduce(lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
1144+
11371145

11381146
@unittest.skipUnless(c_functools, 'requires the C _functools module')
11391147
class TestReduceC(TestReduce, unittest.TestCase):
@@ -1144,12 +1152,6 @@ class TestReduceC(TestReduce, unittest.TestCase):
11441152
class TestReducePy(TestReduce, unittest.TestCase):
11451153
reduce = staticmethod(py_functools.reduce)
11461154

1147-
def test_reduce_with_kwargs(self):
1148-
with self.assertWarns(DeprecationWarning):
1149-
self.reduce(function=lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
1150-
with self.assertWarns(DeprecationWarning):
1151-
self.reduce(lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
1152-
11531155

11541156
class TestCmpToKey:
11551157

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove kw parameters from python version of :func:`functools.reduce`
2+
function.

0 commit comments

Comments
 (0)