Skip to content

Using the function argument of itertools.accumulate() gets error against the doc #135282

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

Open
hyperkai opened this issue Jun 9, 2025 · 1 comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@hyperkai
Copy link

hyperkai commented Jun 9, 2025

Bug report

Bug description:

The doc of itertools.accumulate() says the 2nd parameter is function as shown below:

itertools.accumulate(iterable[, function, *, initial=None])

But using function argument gets the error against the doc as shown below:

from itertools import accumulate
from operator import mul
                                            # ↓↓↓↓↓↓↓↓
for x in accumulate(iterable=[1, 2, 3, 4, 5], function=mul):
    print(x)
# TypeError: 'function' is an invalid keyword argument for accumulate()

So, I used func argument, then it works as shown below:

from itertools import accumulate
from operator import mul
                                            # ↓↓↓↓
for x in accumulate(iterable=[1, 2, 3, 4, 5], func=mul):
    print(x)
# 1
# 2
# 6
# 24
# 120

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@hyperkai hyperkai added the type-bug An unexpected behavior, bug, or error label Jun 9, 2025
@skirpichev skirpichev added the docs Documentation in the Doc dir label Jun 9, 2025
@skirpichev
Copy link
Contributor

That's because many functions in the itertools docs have funny syntax with brackets instead of correct signatures. See also #131885.

But help() and inspect.signature() will show you a correct signature:

>>> help(itertools.accumulate)
Help on class accumulate in module itertools:

class accumulate(builtins.object)
 |  accumulate(iterable, func=None, *, initial=None)
 |
 |  Return series of accumulated sums (or other binary function results).
 |
 |  Methods defined here:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __next__(self, /)
 |      Implement next(self).
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.

In this case - you are using wrong keyword name. Should be func, not function.

skirpichev added a commit to skirpichev/cpython that referenced this issue Jun 9, 2025
Now it's in sync with docstring/inspect output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
Status: Todo
Development

No branches or pull requests

2 participants