Skip to content

itertools: fix count() repr to show float step 1.0#8261

Merged
youknowone merged 1 commit into
RustPython:mainfrom
JaceJung-dev:fix-itertools-count-repr
Jul 13, 2026
Merged

itertools: fix count() repr to show float step 1.0#8261
youknowone merged 1 commit into
RustPython:mainfrom
JaceJung-dev:fix-itertools-count-repr

Conversation

@JaceJung-dev

@JaceJung-dev JaceJung-dev commented Jul 12, 2026

Copy link
Copy Markdown
Contributor
  • This PR follows our AI policy.

Summary

itertools.count()'s __repr__ omitted the step when it equalled 1, but
CPython omits the step only for the integer 1 — a float step of 1.0
should still be shown.

from itertools import count
repr(count(10.5))
# before: 'count(10.5)'
# after:  'count(10.5, 1.0)'   # matches CPython

CPython's count_repr skips the step only when it is a PyLong equal to 1,
so a float such as 1.0 is still shown. The repr now omits the step only when it
is exactly the integer 1 (checked via both the int type and value). Also
unmarks test_count_with_step, which now passes.

Verified locally:

  • cargo run --release -- -m unittest -v test.test_itertools.TestBasicOps.test_count_with_step -> ok
  • cargo run --release -- -m test test_itertools -> SUCCESS (run=137, skipped=22)

Assisted-by: Claude Code:claude-opus-4-8

Summary by CodeRabbit

  • Bug Fixes
    • Improved the display of iterator count expressions.
    • The step value is now omitted only when it is exactly the integer 1; other values and types remain explicitly shown for accurate representations.

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 8a298d31-2e84-413f-a9d4-282624651c61

📥 Commits

Reviewing files that changed from the base of the PR and between d9f76ac and 51b83c4.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_itertools.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/stdlib/itertools.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/vm/src/stdlib/itertools.rs

📝 Walkthrough

Walkthrough

PyItertoolsCount::repr_wtf8 precomputes whether step is an integer equal to one, then uses that predicate to decide whether to include step in the count(...) representation.

Changes

Count representation

Layer / File(s) Summary
Step formatting predicate
crates/vm/src/stdlib/itertools.rs
repr_wtf8 checks the step type and equality with 1 before omitting the step from the count(...) representation.

Estimated code review effort: 1 (Trivial) | ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: count() repr now includes a float step like 1.0 instead of omitting it.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] test: cpython/Lib/test/test_itertools.py (TODO: 6)

dependencies:

dependent tests: (56 tests)

  • itertools: test_annotationlib test_ast test_asyncio test_bdb test_buffer test_builtin test_call test_codeccallbacks test_collections test_compile test_concurrent_futures test_csv test_ctypes test_descr test_dis test_email test_exceptions test_functools test_genericalias test_hashlib test_heapq test_httplib test_importlib test_inspect test_io test_iterlen test_itertools test_launcher test_logging test_math test_memoryview test_mmap test_os test_peepholer test_platform test_pprint test_pyrepl test_queue test_range test_set test_shlex test_slice test_socket test_sort test_statistics test_str test_struct test_subprocess test_tokenize test_tuple test_typing test_unittest test_uuid test_winreg test_xml_etree test_zipfile

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone youknowone added the z-ca-2026 Tag to track Contribution Academy 2026 label Jul 12, 2026

@moreal moreal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, it looks like your changes correspond to these lines:

https://github.com/python/cpython/blob/c63aec69bd59c55314c06c23f4c22c03de76fe45/Modules/itertoolsmodule.c#L3555-L3569

Everything seems to pass the tests on my local machine (cargo run -- -m test test_itertools -m test_count_with_step -v), so I'm leaving an approval. Thanks for your contribution!

However, the CI is currently failing. I believe rebasing onto the latest main branch should resolve this issue.

Assisted-by: Claude Code:claude-opus-4-8
@JaceJung-dev JaceJung-dev force-pushed the fix-itertools-count-repr branch from d9f76ac to 51b83c4 Compare July 12, 2026 13:12
@JaceJung-dev

Copy link
Copy Markdown
Contributor Author

Thanks for the review and approval! 🙏
I've rebased onto the latest main(which includes the clippy fix in #8249) to resolve the CI failure.
Thanks for the helpful guidance — much appreciated!

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tysm!

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 welcome to RustPython project

@youknowone youknowone merged commit 25813c2 into RustPython:main Jul 13, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants