← 输入框|github.com|刷新
Skip to content

Add explicit Python collection conversion adapters - #9372

Open
1ndahous3 wants to merge 3 commits into
RustPython:mainfrom
1ndahous3:py_collection_adapters
Open

1ndahous3 wants to merge 3 commits into
RustPython:mainfrom
1ndahous3:py_collection_adapters

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

This proposal comes from my personal project, br3k, which uses an embedded RustPython VM:

The call_between_rust_and_python example also had its own list conversion wrapper.

Summary

  • Add PyListOf<T>, PySetOf<T> and PyDictOf<K, V> for constructing Python collections from Rust iterators. Explicit list conversion includes u8 without changing Vec<u8> conversion to bytes.
  • Use Python hashing and equality for sets and dictionaries, propagating construction errors and stopping at the failing item.
  • Preserve collection identity when cloning or returning an adapter, with GC traversal of the owned reference.
  • Keep element access untyped: type parameters describe construction, while Python can subsequently mutate the collections.
  • Replace manual collection conversion in CSV dialect listing, signal sets, and sys._current_frames() / sys._current_exceptions() while preserving snapshots and object identity.
  • Show built-in list conversion alongside a custom domain conversion in the embedding example.

Example

let labels = PyListOf::new(["ready", "done"], vm);
let flags = PySetOf::try_new([1, 2, 1], vm)?;
let counts = PyDictOf::try_new([("ready", 2), ("done", 1)], vm)?;

AI assistance

Written with Codex (GPT-6), reviewed by a human before submission.

Summary by CodeRabbit

  • New Features
    • Added typed helpers for creating Python lists, dictionaries, and sets from Rust values. Dictionary keys and set items follow Python equality and duplicate-handling behavior.
    • Expanded the Rust–Python interoperability example with ordered measurements and list-based values.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T22:26:25.239861Z 10d7ff6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0dca682 and 10d7ff6.

Files selected for processing: 9
  • crates/stdlib/src/csv.rs
  • crates/vm/src/convert/dict.rs
  • crates/vm/src/convert/list.rs
  • crates/vm/src/convert/mod.rs
  • crates/vm/src/convert/set.rs
  • crates/vm/src/stdlib/_signal.rs
  • crates/vm/src/stdlib/sys.rs
  • examples/call_between_rust_and_python.py
  • examples/call_between_rust_and_python.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The change adds typed Rust wrappers for Python dictionaries, lists, and sets, exports them from the VM conversion module, and uses them in standard-library functions and a Rust-to-Python example. The wrappers convert iterator inputs and expose the underlying Python containers.

Changes: Typed Container Conversions

Layer / File(s) Summary
Typed container wrappers
crates/vm/src/convert/*
The conversion module exports PyDictOf, PyListOf, and PySetOf. The wrappers construct Python containers from Rust inputs and provide access, conversion, cloning, and traversal support, with tests for their behavior.
Standard-library container construction
crates/stdlib/src/csv.rs, crates/vm/src/stdlib/_signal.rs, crates/vm/src/stdlib/sys.rs
list_dialects uses PyListOf; signal functions construct sets with PySetOf; _current_frames and _current_exceptions construct dictionaries with PyDictOf.
Rust-to-Python example
examples/call_between_rust_and_python.rs, examples/call_between_rust_and_python.py
The example exposes numbers as a Python list and measurements as a list of pairs. The Python callback prints the measurements and flushes the output.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Possibly related PRs

Suggested reviewers: youknowone

Merge Risk: Minimal · up to 10d7f

No specific issue remains that should block merging after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 42 functions across 9 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 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: adding explicit adapters for Python collection conversions.
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 a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 10d7ff6d53

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/vm/src/convert/list.rs
@youknowone

Copy link
Copy Markdown
Member

this is interesting idea, but let me look in details.

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.

2 participants