Skip to content
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

gh-121676: Raise a DeprecationWarning if the Python implementation of functools.reduce is called with a keyword args #121677

Merged
merged 14 commits into from
Jan 1, 2025
27 changes: 22 additions & 5 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,6 @@ def reduce(function, sequence, initial=_initial_missing):

return value

try:
from _functools import reduce
except ImportError:
pass


################################################################################
### partial() argument application
Expand Down Expand Up @@ -1122,3 +1117,25 @@ def __get__(self, instance, owner=None):
return val

__class_getitem__ = classmethod(GenericAlias)

def _warn_kwargs(func):
picnixz marked this conversation as resolved.
Show resolved Hide resolved
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
@wraps(func)
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
def wrapper(*args, **kwargs):
if kwargs:
import os
import warnings
warnings.warn(
'Calling functools.reduce with keyword arguments '
'is deprecated in Python 3.14 and will be '
'forbidden in Python 3.16.',
DeprecationWarning,
skip_file_prefixes=(os.path.dirname(__file__),))
return func(*args, **kwargs)
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
return wrapper

reduce = _warn_kwargs(reduce)
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved

try:
from _functools import reduce
except ImportError:
pass
6 changes: 6 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,12 @@ 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)
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
with self.assertWarns(DeprecationWarning):
self.reduce(lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)


class TestCmpToKey:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Now calling the Python implementation of the :meth:`functools.reduce` with a
:term:`keyword argument` is raises a :exc:`DeprecationWarning`.
This will be forbidden in Python 3.16 in order to match the C implementation.
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
Loading