Skip to content

Fix itertools.count() accepting None for start and step - #1262

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/itertools-count-none
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/itertools-count-none

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Fixes #1251

CountNode declared start and step with useDefaultForNone = true, so an explicit None was replaced by the default before the number check. CPython defaults only an omitted argument and rejects None in itertools_count_impl (itertoolsmodule.c#L4022-L4026).

AS-IS

>>> itertools.count(None)
count(0)
>>> itertools.count(0, None)
count(0)
>>> itertools.count(step=None)
count(0)

TO-BE

>>> itertools.count(None)
TypeError: a number is required
>>> itertools.count(0, None)
TypeError: a number is required
>>> itertools.count(step=None)
TypeError: a number is required

(matching CPython)

Changes

  • Drop useDefaultForNone = true from the start and step annotations of CountNode, so the defaults apply only to an omitted argument.
  • Add tests/test_itertools_count.py.

Testing

mx python-jvm on darwin-aarch64, then the new test_itertools_count.py (3 passed) and the tagged test_itertools (124 passed). The new test fails on the unpatched build. count(), count(5), count(1.5, 2) and count(step=2) print the same as before, and count(start=None) now raises TypeError like CPython 3.13.14.

Both arguments were declared with useDefaultForNone, so an explicit
None was replaced by the default and count(None) and count(0, None)
returned count(0). CPython requires a number for any argument that
is passed and raises TypeError for None.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

1 participant