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-125916: Allow functools.reduce 'initial' to be a keyword argument #125917

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
7 changes: 5 additions & 2 deletions Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ The :mod:`functools` module defines the following functions:
.. versionadded:: 3.4


.. function:: reduce(function, iterable[, initial], /)
.. function:: reduce(function, iterable, /[, initial])

Apply *function* of two arguments cumulatively to the items of *iterable*, from
left to right, so as to reduce the iterable to a single value. For example,
Expand All @@ -468,7 +468,7 @@ The :mod:`functools` module defines the following functions:

initial_missing = object()

def reduce(function, iterable, initial=initial_missing, /):
def reduce(function, iterable, /, initial=initial_missing):
sayandipdutta marked this conversation as resolved.
Show resolved Hide resolved
it = iter(iterable)
if initial is initial_missing:
value = next(it)
Expand All @@ -481,6 +481,9 @@ The :mod:`functools` module defines the following functions:
See :func:`itertools.accumulate` for an iterator that yields all intermediate
values.

.. versionchanged:: next
*initial* is now supported as a keyword argument.

.. decorator:: singledispatch

Transform a function into a :term:`single-dispatch <single
Expand Down
3 changes: 2 additions & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ def __ge__(self, other):

def reduce(function, sequence, initial=_initial_missing):
"""
reduce(function, iterable[, initial], /) -> value
reduce(function, iterable, /,
initial=_functools._initial_missing) -> value
sayandipdutta marked this conversation as resolved.
Show resolved Hide resolved

Apply a function of two arguments cumulatively to the items of a sequence
or iterable, from left to right, so as to reduce the iterable to a single
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,29 @@ def __getitem__(self, i):
d = {"one": 1, "two": 2, "three": 3}
self.assertEqual(self.reduce(add, d), "".join(d.keys()))

# test correctness of keyword usage of `initial` in `reduce`
def test_initial_keyword(self):
def add(x, y):
return x + y
self.assertEqual(
self.reduce(add, ['a', 'b', 'c'], ''),
self.reduce(add, ['a', 'b', 'c'], initial=''),
)
self.assertEqual(
self.reduce(add, [['a', 'c'], [], ['d', 'w']], []),
self.reduce(add, [['a', 'c'], [], ['d', 'w']], initial=[]),
)
self.assertEqual(
self.reduce(lambda x, y: x*y, range(2,8), 1),
self.reduce(lambda x, y: x*y, range(2,8), initial=1),
)
self.assertEqual(
self.reduce(lambda x, y: x*y, range(2,21), 1),
self.reduce(lambda x, y: x*y, range(2,21), initial=1),
)
self.assertRaises(TypeError, self.reduce, add, [0, 1], initial="")
self.assertEqual(self.reduce(42, "", initial="1"), "1") # func is never called with one item


@unittest.skipUnless(c_functools, 'requires the C _functools module')
class TestReduceC(TestReduce, unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5697,8 +5697,8 @@ def test_faulthandler_module_has_signatures(self):
self._test_module_has_signatures(faulthandler, unsupported_signature=unsupported_signature)

def test_functools_module_has_signatures(self):
no_signature = {'reduce'}
self._test_module_has_signatures(functools, no_signature)
unsupported_signature = {"reduce"}
self._test_module_has_signatures(functools, unsupported_signature=unsupported_signature)

def test_gc_module_has_signatures(self):
import gc
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ Luke Dunstan
Virgil Dupras
Bruno Dupuis
Andy Dustman
Sayandip Dutta
Gary Duzan
Eugene Dvurechenski
Karmen Dykstra
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow the *initial* argument of :func:`functools.reduce` to be a keyword.
sayandipdutta marked this conversation as resolved.
Show resolved Hide resolved
Patch by Sayandip Dutta.
46 changes: 29 additions & 17 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,15 +932,31 @@ _functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)

/* reduce (used to be a builtin) ********************************************/

// Not converted to argument clinic, because of `args` in-place modification.
// AC will affect performance.
/*[clinic input]
_functools.reduce

function as func: object
iterable as seq: object
/
initial as result: object(c_default="NULL") = _functools._initial_missing

Apply a function of two arguments cumulatively to an iterable, from left to right.

This efficiently reduces the iterable to a single value. If initial is present,
it is placed before the items of the iterable in the calculation, and serves as
a default when the iterable is empty.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
calculates ((((1 + 2) + 3) + 4) + 5).
[clinic start generated code]*/

static PyObject *
functools_reduce(PyObject *self, PyObject *args)
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
PyObject *result)
/*[clinic end generated code: output=30d898fe1267c79d input=968b8e45b819c0da]*/
{
PyObject *seq, *func, *result = NULL, *it;
PyObject *args, *it;

if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
return NULL;
if (result != NULL)
Py_INCREF(result);

Expand Down Expand Up @@ -1006,16 +1022,6 @@ functools_reduce(PyObject *self, PyObject *args)
return NULL;
}

PyDoc_STRVAR(functools_reduce_doc,
"reduce(function, iterable[, initial], /) -> value\n\
\n\
Apply a function of two arguments cumulatively to the items of a sequence\n\
or iterable, from left to right, so as to reduce the iterable to a single\n\
value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
of the iterable in the calculation, and serves as a default when the\n\
iterable is empty.");

/* lru_cache object **********************************************************/

/* There are four principal algorithmic differences from the pure python version:
Expand Down Expand Up @@ -1720,7 +1726,7 @@ PyDoc_STRVAR(_functools_doc,
"Tools that operate on functions.");

static PyMethodDef _functools_methods[] = {
{"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
_FUNCTOOLS_REDUCE_METHODDEF
_FUNCTOOLS_CMP_TO_KEY_METHODDEF
{NULL, NULL} /* sentinel */
};
Expand Down Expand Up @@ -1789,6 +1795,12 @@ _functools_exec(PyObject *module)
// lru_list_elem is used only in _lru_cache_wrapper.
// So we don't expose it in module namespace.

if (PyModule_Add(module, "_initial_missing",
PyObject_New(PyObject, &PyBaseObject_Type)) < 0)
{
return -1;
}

return 0;
}

Expand Down
75 changes: 74 additions & 1 deletion Modules/clinic/_functoolsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading