Skip to content

times argument of itertools.repeat() accepts a negative number and doesn't accept None #135330

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

Closed
hyperkai opened this issue Jun 10, 2025 · 2 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@hyperkai
Copy link

hyperkai commented Jun 10, 2025

Bug report

Bug description:

times argument of itertools.repeat() accepts a negative number as shown below:

from itertools import repeat
                               # ↓↓↓↓
v = repeat(object='Hello', times=-100)

So, error should be raised like below:

ValueError: times must be 0 <= x

And, times argument doesn't accept None as shown below so None should be accepted:

from itertools import repeat

v = repeat(object='Hello', times=None)
# TypeError: 'NoneType' object cannot be interpreted as an integer

CPython versions tested on:

3.13

Operating systems tested on:

Windows

@hyperkai hyperkai added the type-bug An unexpected behavior, bug, or error label Jun 10, 2025
@skirpichev
Copy link
Contributor

times argument of itertools.repeat() accepts a negative number as shown below:

Why not? range also accepts negative stop argument:

>>> list(range(-10))
[]
>>> import itertools
>>> list(itertools.repeat('a', 2))
['a', 'a']
>>> list(itertools.repeat('a', -2))
[]

And, times argument doesn't accept None as shown below

Example below is just an illustration. It's not API description.

Rather, function signature should be described like for range:

.. function:: repeat(object)
              repeat(object, times)

@skirpichev skirpichev added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error invalid labels Jun 10, 2025
@skirpichev
Copy link
Contributor

I opened a discussion thread: https://discuss.python.org/t/how-to-document-callables-with-multiple-signatures/95015

Meanwhile, this will be closed, as we don't have rules for functions with multiple signatures.

@skirpichev skirpichev closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2025
@skirpichev skirpichev self-assigned this Jun 10, 2025
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
Projects
Status: Todo
Development

No branches or pull requests

2 participants