Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ShaharNaveh
Copy link
Collaborator

@ShaharNaveh ShaharNaveh commented Sep 1, 2025

Summary by CodeRabbit

  • Refactor

    • Unified the expression stringification API behind a single constructor and updated internal call sites for consistency.
    • Preserves existing output formatting; no user-facing behavior changes.
  • Style

    • Minor internal initialization cleanup with no functional impact.

Copy link
Contributor

coderabbitai bot commented Sep 1, 2025

Walkthrough

Replaces the free function unparse::unparse_expr with the associated constructor UnparseExpr::new across the codegen. Updates compile.rs call sites accordingly. Removes the old function, adds the constructor, and keeps formatting behavior unchanged via Display on UnparseExpr.

Changes

Cohort / File(s) Summary
Unparse API migration (call sites)
compiler/codegen/src/compile.rs
Updated imports to use UnparseExpr. Replaced unparse_expr(...) with UnparseExpr::new(...).to_string() at two locations (mapping pattern key and annotation expression).
Unparse module API change
compiler/codegen/src/unparse.rs
Removed pub const fn unparse_expr(...). Added impl UnparseExpr::new(...). Updated internal call paths to construct UnparseExpr via new. Minor constructor style change in Unparser::new initialization; behavior unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller as Codegen caller
  participant UE as UnparseExpr
  participant Disp as Display
  participant U as Unparser

  Caller->>UE: UnparseExpr::new(expr, source)
  Note right of UE: Stores expr/source
  Caller->>Disp: to_string() on UnparseExpr
  Disp->>U: Unparser::new(f, source)
  U-->>Disp: write formatted expr
  Disp-->>Caller: String
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch my whiskers, flip the switch,
A function hops to method niche.
New burrow: UnparseExpr::new,
Same trails, same views, shining through.
Code carrots lined in tidy rows—
A gentle refactor, onward it goes. 🥕✨

✨ 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.

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: 1

📜 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 fa91df6 and 82fc735.

📒 Files selected for processing (2)
  • compiler/codegen/src/compile.rs (3 hunks)
  • compiler/codegen/src/unparse.rs (7 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:

  • compiler/codegen/src/compile.rs
  • compiler/codegen/src/unparse.rs
🧬 Code graph analysis (1)
compiler/codegen/src/compile.rs (1)
compiler/codegen/src/unparse.rs (2)
  • new (34-36)
  • new (613-615)
⏰ 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). (10)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
🔇 Additional comments (5)
compiler/codegen/src/compile.rs (3)

3595-3604: Call-site migration preserves behavior.

UnparseExpr::new(key, &self.source_file).to_string() is a drop-in replacement for the prior helper. No functional change.


4149-4152: Annotation unparsing change is correct.

UnparseExpr::new(...).to_string().into() properly produces Wtf8Buf.


17-18: Approve import switch—no lingering free function calls
Verified that all unparse_expr( occurrences are method definitions or self.unparse_expr calls in compiler/codegen/src/unparse.rs; no standalone free‐function invocations remain.

compiler/codegen/src/unparse.rs (2)

34-36: Minor constructor style tweak — OK.

Using Self { .. } is idiomatic.


612-616: Public constructor addition — OK.

pub const fn new(...) -> Self mirrors the old free function’s const-ness and keeps lifetimes intact.

Comment on lines +172 to 173
write!(self, ": {}", UnparseExpr::new(body, self.source))?;
})
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

write!(self, ...) requires implementing fmt::Write for Unparser (compile error otherwise).

write! calls target std::fmt::Write::write_fmt, so the destination must implement fmt::Write. Unparser currently does not, which will fail to compile.

Apply this diff to implement fmt::Write and forward to the inner formatter:

@@
 impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
@@
     fn write_fmt(&mut self, f: fmt::Arguments<'_>) -> fmt::Result {
         self.f.write_fmt(f)
     }
 }
 
+impl<'a, 'b, 'c> fmt::Write for Unparser<'a, 'b, 'c> {
+    fn write_str(&mut self, s: &str) -> fmt::Result {
+        self.f.write_str(s)
+    }
+    fn write_char(&mut self, c: char) -> fmt::Result {
+        self.f.write_char(c)
+    }
+}

Alternatively, replace each write!(self, "...", ...) with self.write_fmt(format_args!("...", ...))?, but the trait impl keeps call sites cleaner.

Also applies to: 197-199, 275-277, 285-289, 483-486, 492-494

🤖 Prompt for AI Agents
In compiler/codegen/src/unparse.rs around lines 172-173 (also applies to
197-199, 275-277, 285-289, 483-486, 492-494): the code uses write!(self, ...)
but Unparser does not implement std::fmt::Write so compilation fails; implement
the fmt::Write trait for Unparser and forward its methods to the inner
formatter/collector (i.e., implement fn write_str(&mut self, s: &str) ->
fmt::Result and any other required fmt::Write methods by delegating to the inner
formatter or its write methods), so existing write! call sites compile unchanged
(alternatively replace write! calls with self.write_fmt(format_args!(...)) at
each site if you prefer not to add the trait impl).

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

The change looks good.

On the other hand, Ruff also have the corresponding part. Checking if we can share the implement may be worth

@youknowone youknowone merged commit 1688e74 into RustPython:main Sep 2, 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.

2 participants