gh-125916: Allow functools.reduce() 'initial' to be a keyword argument (#125917)

This commit is contained in:
Sayandip Dutta 2024-11-12 18:41:58 +05:30 committed by GitHub
parent 6e3bb8a913
commit abb90ba46c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 76 additions and 13 deletions

View file

@ -1005,6 +1005,29 @@ class TestReduce:
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):