Skip to content

itertools: defer dropwhile predicate validation - #8504

Open
widehyo1 wants to merge 1 commit into
RustPython:mainfrom
widehyo1:fix-itertools-dropwhile-lazy-callable
Open

itertools: defer dropwhile predicate validation#8504
widehyo1 wants to merge 1 commit into
RustPython:mainfrom
widehyo1:fix-itertools-dropwhile-lazy-callable

Conversation

@widehyo1

@widehyo1 widehyo1 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Defer itertools.dropwhile() predicate validation until iteration, matching
CPython's lazy behavior.

Details

dropwhile stored its predicate as ArgCallable, which rejected a
non-callable predicate during construction. CPython permits construction and
only calls (and therefore validates) the predicate when it consumes an item.
This matters for an empty iterable and for the timing of exceptions.

Store the predicate as PyObjectRef and invoke it from next(). The change
keeps the existing first-false-item behavior and exception propagation while
aligning validation timing with CPython.

TestBasicOps.test_dropwhile now passes with its existing CPython test body,
so remove its expectedFailure marker.

Testing

  • uv tool run prek run --all-files
  • cargo fmt --check
  • cargo run --release Lib/test/test_itertools.py
    • 137 tests run
    • 21 skipped
    • 5 expected failures
  • cargo run --release -- -m test test_itertools
  • cargo run -- extra_tests/snippets/stdlib_itertools.py
  • cargo clippy -p rustpython-vm --all-targets -- -Dwarnings
  • cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --exclude rustpython-capi
  • git diff --check

AI assistance: Codex:gpt-5.6-sol

Summary by CodeRabbit

  • Bug Fixes
    • Improved dropwhile predicate handling for more reliable iterator behavior.
    • Removed unnecessary callable wrapping during predicate evaluation.

Store the `dropwhile` predicate as a Python object and call it while
advancing the iterator. This defers callable validation until the predicate
is first needed, matching CPython for empty input while preserving exception
propagation during iteration.

Remove the now-passing `test_dropwhile` expected-failure marker.

Assisted-by: Codex:gpt-5.6-sol
@coderabbitai

coderabbitai Bot commented Aug 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 Plus

Run ID: b2cfe042-dd56-4d9a-989a-501a0318ff4a

📥 Commits

Reviewing files that changed from the base of the PR and between 24bd3b3 and d918a7e.

⛔ 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

📝 Walkthrough

Walkthrough

The dropwhile implementation now stores predicates as PyObjectRef and calls them directly during iteration. The unused ArgCallable import was removed.

Changes

Dropwhile predicate handling

Layer / File(s) Summary
Store and invoke predicates
crates/vm/src/stdlib/itertools.rs
Dropwhile and DropwhileNewArgs store predicates as PyObjectRef. Predicate evaluation uses call instead of cloning and invoking an ArgCallable.

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

Suggested reviewers: shaharnaveh

🚥 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 clearly and concisely describes the main change: deferring predicate validation in itertools.dropwhile.
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: 4)

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

@moreal moreal added the z-ca-2026 Tag to track Contribution Academy 2026 label Aug 12, 2026
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.

2 participants