Skip to content

Conversation

@ever0de
Copy link
Contributor

@ever0de ever0de commented Sep 1, 2025

Summary by CodeRabbit

  • New Features
    • Cursor.fetchmany now accepts a size keyword argument (via an argument bag) and uses arraysize when omitted.
  • Bug Fixes
    • Clearer handling of invalid argument combinations (too many positional args, unexpected keywords, or duplicate size).
    • Behavior: non‑positive size fetches all remaining rows; positive size limits returned rows.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 1, 2025

Walkthrough

Cursor.fetchmany in stdlib/src/sqlite.rs was refactored to accept a typed argument bag (FetchManyArgs) with optional size, derive parsing from args, default to arraysize when absent, use Cursor::next for iteration, and apply the row limit only when max_rows > 0.

Changes

Cohort / File(s) Summary
SQLite Cursor API refactor
stdlib/src/sqlite.rs
Added FetchManyArgs { size: Option<c_int> } (FromArgs), changed Cursor::fetchmany signature to take FetchManyArgs, derive max_rows from args.size or zelf.arraysize, switch loop to Cursor::next(zelf, vm)?, and only enforce the fetch cap when max_rows > 0.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor PyCode as Python Code
  participant Cursor as Cursor.fetchmany
  participant Args as FetchManyArgs parser
  participant DB as SQLite Cursor::next

  PyCode->>Cursor: fetchmany(size=?)
  Cursor->>Args: parse into FetchManyArgs (optional size)
  Args-->>Cursor: size or None
  alt parse error
    Cursor-->>PyCode: raise TypeError/ValueError
  else valid
    Cursor->>DB: Cursor::next repeatedly
    loop until rows exhausted or (max_rows > 0 and count == max_rows)
      DB-->>Cursor: next row
      Cursor-->>PyCode: accumulate rows
    end
    Cursor-->>PyCode: return list of rows
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I twitch my nose at args made clean,
One little size, optional and keen.
Arraysize waits when none is shown,
I hop through rows till limit’s known.
Cursor::next — snug, steady, and bright.


📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between a2b194a and 2e16f51.

📒 Files selected for processing (1)
  • stdlib/src/sqlite.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • stdlib/src/sqlite.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run tests under miri
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

fn fetchmany(
zelf: &Py<Self>,
max_rows: OptionalArg<c_int>,
mut args: FuncArgs,
Copy link
Member

Choose a reason for hiding this comment

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

Can #[derive(FromArgs)] be applicable? Or is there any blocker not allowing to do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can change it. However, I believe we can't use the FromArgs derive macro because the error messages would be different.

2e16f51 (#6118)

 --- Testing with too many positional arguments ---
-fetchmany() takes from 0 to 1 positional arguments but 2 were given  (cpython)
+expected at most 2 arguments, got 3 (FromArgs)
 
 --- Testing with an unexpected keyword argument ---
-fetchmany() got an unexpected keyword argument 'unexpected_kw' (cpython)
+Unexpected keyword argument unexpected_kw (FromArgs)
 
 --- Testing with both positional and keyword for the same argument ---
-fetchmany() got multiple values for argument 'size' (cpython)
+Unexpected keyword argument size (FromArgs)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you think the current error messages are acceptable, I believe we can apply this version as is.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, those error messages are fine - there's discrepancy in so many places, that if we wanted to make the error messages line up we'd just change FromArgs all at once.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
stdlib/src/sqlite.rs (2)

1700-1705: Quote unexpected keyword name to match CPython error text.

CPython formats this as "got an unexpected keyword argument 'name'". Minor UX polish and closer compatibility.

Apply this diff:

-            if let Some((name_str, _)) = args.kwargs.into_iter().next() {
-                return Err(vm.new_type_error(format!(
-                    "fetchmany() got an unexpected keyword argument {name_str}",
-                )));
-            }
+            if let Some((name, _)) = args.kwargs.into_iter().next() {
+                return Err(vm.new_type_error(format!(
+                    "fetchmany() got an unexpected keyword argument '{}'",
+                    name.as_str(),
+                )));
+            }

1706-1715: Semantics match CPython for size <= 0; optional micro-optimizations.

  • The “only enforce when max_rows > 0” behavior aligns with CPython’s implementation, where a non-positive size results in unbounded fetching (counter break check never triggers). (chromium.googlesource.com)
  • Micro: preallocate when max_rows > 0 to reduce reallocations.

Apply this diff:

-            let mut list = vec![];
+            let mut list = if max_rows > 0 {
+                Vec::with_capacity(max_rows as usize)
+            } else {
+                Vec::new()
+            };

No change in behavior; marginal perf improvement when many rows are fetched.

Also applies to: 1720-1720

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 7509387 and a2b194a.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_sqlite3/test_dbapi.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • stdlib/src/sqlite.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style (cargo fmt to format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • stdlib/src/sqlite.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run tests under miri
🔇 Additional comments (1)
stdlib/src/sqlite.rs (1)

1685-1689: Good change: accept keyword arg size via FuncArgs.

The manual parse cleanly supports both positional and keyword forms and keeps the default from arraysize when omitted. Looks consistent with the rest of the module’s argument handling.

Copy link
Member

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

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

LGTM!

@coolreader18 coolreader18 merged commit fa91df6 into RustPython:main Sep 1, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants