]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-149537: Remove kw parameters from python version of `reduce` (#149538)
authorsobolevn <mail@sobolevn.me>
Fri, 8 May 2026 15:34:48 +0000 (18:34 +0300)
committerGitHub <noreply@github.com>
Fri, 8 May 2026 15:34:48 +0000 (18:34 +0300)
Doc/whatsnew/3.16.rst
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2026-05-08-08-24-01.gh-issue-149537.hVFVnt.rst [new file with mode: 0644]

index 98a8644884a8d7e84d9cd310eaa0a2dae54b80a3..4ddc836d9b29e485d1566547f71fa8bbd24751b3 100644 (file)
@@ -114,6 +114,12 @@ annotationlib
   Use :meth:`annotationlib.ForwardRef.evaluate`
   or :func:`typing.evaluate_forward_ref` instead.
 
+functools
+---------
+
+* Calling the Python implementation of :func:`functools.reduce` with *function*
+  or *sequence* as keyword arguments has been deprecated since Python 3.14.
+
 sysconfig
 ---------
 
index cd374631f167925b9a495a2d6e6d75d910a80ebb..73274b94ef37da0dfe2eb812206392e614c58528 100644 (file)
@@ -234,7 +234,7 @@ except ImportError:
 
 _initial_missing = object()
 
-def reduce(function, sequence, initial=_initial_missing):
+def reduce(function, sequence, /, initial=_initial_missing):
     """
     reduce(function, iterable, /[, initial]) -> value
 
@@ -264,6 +264,11 @@ def reduce(function, sequence, initial=_initial_missing):
 
     return value
 
+try:
+    from _functools import reduce
+except ImportError:
+    pass
+
 
 ################################################################################
 ### partial() argument application
@@ -1178,31 +1183,3 @@ class cached_property:
         return val
 
     __class_getitem__ = classmethod(GenericAlias)
-
-def _warn_python_reduce_kwargs(py_reduce):
-    @wraps(py_reduce)
-    def wrapper(*args, **kwargs):
-        if 'function' in kwargs or 'sequence' in kwargs:
-            import os
-            import warnings
-            warnings.warn(
-                'Calling functools.reduce with keyword arguments '
-                '"function" or "sequence" '
-                'is deprecated in Python 3.14 and will be '
-                'forbidden in Python 3.16.',
-                DeprecationWarning,
-                skip_file_prefixes=(os.path.dirname(__file__),))
-        return py_reduce(*args, **kwargs)
-    return wrapper
-
-reduce = _warn_python_reduce_kwargs(reduce)
-del _warn_python_reduce_kwargs
-
-# The import of the C accelerated version of reduce() has been moved
-# here due to gh-121676. In Python 3.16, _warn_python_reduce_kwargs()
-# should be removed and the import block should be moved back right
-# after the definition of reduce().
-try:
-    from _functools import reduce
-except ImportError:
-    pass
index a8ee7d119e4bc6101aa29b4870cc513e1277408b..c30386afe41849b4b262db197135024cdfc52930 100644 (file)
@@ -1134,6 +1134,14 @@ class TestReduce:
         self.assertRaises(TypeError, self.reduce, add, [0, 1], initial="")
         self.assertEqual(self.reduce(42, "", initial="1"), "1") # func is never called with one item
 
+    def test_reduce_with_kwargs(self):
+        with self.assertRaises(TypeError):
+            self.reduce(function=lambda x, y: (x or 1) + y, sequence=[1, 2, 3, 4, 5])
+        with self.assertRaises(TypeError):
+            self.reduce(function=lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
+        with self.assertRaises(TypeError):
+            self.reduce(lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
+
 
 @unittest.skipUnless(c_functools, 'requires the C _functools module')
 class TestReduceC(TestReduce, unittest.TestCase):
@@ -1144,12 +1152,6 @@ class TestReduceC(TestReduce, unittest.TestCase):
 class TestReducePy(TestReduce, unittest.TestCase):
     reduce = staticmethod(py_functools.reduce)
 
-    def test_reduce_with_kwargs(self):
-        with self.assertWarns(DeprecationWarning):
-            self.reduce(function=lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
-        with self.assertWarns(DeprecationWarning):
-            self.reduce(lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
-
 
 class TestCmpToKey:
 
diff --git a/Misc/NEWS.d/next/Library/2026-05-08-08-24-01.gh-issue-149537.hVFVnt.rst b/Misc/NEWS.d/next/Library/2026-05-08-08-24-01.gh-issue-149537.hVFVnt.rst
new file mode 100644 (file)
index 0000000..2fb0359
--- /dev/null
@@ -0,0 +1,2 @@
+Remove kw parameters from python version of :func:`functools.reduce`
+function.