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

Skip to content

Constify some methods and general code cleanups #5812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2025

Conversation

ShaharNaveh
Copy link
Contributor

@ShaharNaveh ShaharNaveh commented Jun 21, 2025

Summary by CodeRabbit

  • New Features

    • Added methods to check if certain string types are empty.
  • Improvements

    • Several methods and constructors across the codebase can now be used in constant expressions, improving compile-time evaluation.
    • Enhanced code readability with formatting adjustments and more concise implementations in some methods.
  • Style

    • Minor formatting changes for improved code clarity and consistency.

Copy link

coderabbitai bot commented Jun 21, 2025

Walkthrough

The changes introduce several const fn method qualifiers across various modules, enabling compile-time evaluation for constructors and utility methods. Additional blank lines are added throughout for formatting and readability. The PyStr type and related wrappers gain new is_empty methods. Minor refactoring and simplification occur in buffer shape checking logic.

Changes

File(s) Change Summary
vm/src/builtins/str.rs Added is_empty methods to PyStr and related wrappers; switched string literal to raw; formatting.
vm/src/function/argument.rs Made KwArgs<T>::new and PosArgs<T>::new constructors const; added blank lines for readability.
vm/src/function/buffer.rs Made len and is_empty methods in ArgBytesLike and ArgMemoryBuffer const.
vm/src/object/core.rs, vm/src/object/ext.rs, Added blank lines inside various impl/trait blocks for readability.
vm/src/object/traverse.rs Added a blank line after Traverse impl for 1-tuple.
vm/src/protocol/buffer.rs Refactored is_zero_in_shape to use iterator .any() for conciseness.
vm/src/protocol/iter.rs Made PyIterIter::new a const fn; added a blank line in a Deref impl.
vm/src/protocol/number.rs Made PyObject::to_number and PyNumber::obj methods const.
vm/src/types/slot.rs Made PyComparisonOp::operator_token a const fn.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant PyStr
    Caller->>PyStr: is_empty()
    PyStr-->>Caller: returns true if underlying data is empty
Loading

Suggested reviewers

  • youknowone

Poem

In fields of code where constness grows,
A bunny hops where compile-time flows.
Methods now swift, with blank lines for grace,
Strings can be empty, checked in their place.
With tidy new order and logic unchanged,
The garden of code is freshly arranged!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f0e7427 and 18b385e.

📒 Files selected for processing (10)
  • vm/src/builtins/str.rs (9 hunks)
  • vm/src/function/argument.rs (4 hunks)
  • vm/src/function/buffer.rs (2 hunks)
  • vm/src/object/core.rs (1 hunks)
  • vm/src/object/ext.rs (4 hunks)
  • vm/src/object/traverse.rs (1 hunks)
  • vm/src/protocol/buffer.rs (1 hunks)
  • vm/src/protocol/iter.rs (2 hunks)
  • vm/src/protocol/number.rs (2 hunks)
  • vm/src/types/slot.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run tests under miri
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
🔇 Additional comments (18)
vm/src/object/traverse.rs (1)

161-161: LGTM! Good formatting improvement.

Adding a blank line here provides better visual separation between the manual 1-tuple Traverse implementation and the macro-generated implementations that follow.

vm/src/types/slot.rs (1)

1119-1119: Excellent const fn addition.

Making operator_token a const fn is beneficial as it enables compile-time evaluation for a method that only returns string literals. This allows the method to be used in const contexts and aligns with the PR's objective of adding const fn qualifiers where appropriate.

vm/src/protocol/buffer.rs (1)

381-381: Nice refactor to idiomatic Rust.

The change from an explicit loop to using .any() makes the code more concise and idiomatic while preserving the exact same logic. This is a good example of leveraging Rust's iterator methods for cleaner code.

vm/src/object/ext.rs (1)

50-50: LGTM! Consistent formatting improvements.

The added blank lines improve readability by providing visual separation within the impl blocks. These formatting changes align well with the PR's code cleanup objectives.

Also applies to: 76-76, 113-113, 187-187

vm/src/builtins/str.rs (4)

530-534: Use of raw string literal for error formatting
Switching to a raw string literal here simplifies the message by removing escape clutter and is fully equivalent.


1858-1862: Implement is_empty for AnyStrWrapper<Wtf8>
This addition keeps the wrapper API consistent with PyStr and other containers.


1868-1872: Implement is_empty for AnyStrWrapper<str>
Maintains symmetry across all AnyStrWrapper implementations.


1878-1882: Implement is_empty for AnyStrWrapper<AsciiStr>
Ensures the ASCII‐only wrapper also has an emptiness check.

vm/src/object/core.rs (1)

494-494: Skip formatting-only blank line addition
This blank line improves readability but does not impact functionality.

vm/src/function/argument.rs (3)

305-305: LGTM: Formatting improvements enhance readability.

The blank lines around trait implementations improve code organization and readability.

Also applies to: 312-312, 359-359, 365-365


347-349: LGTM: Appropriate constification of constructor.

Making KwArgs::new a const fn is correct since it's a simple wrapper constructor that can be evaluated at compile time. This enables better optimization opportunities.


415-417: LGTM: Appropriate constification of constructor.

Making PosArgs::new a const fn is correct since it's a simple wrapper constructor that can be evaluated at compile time. This aligns with the pattern established for KwArgs::new.

vm/src/protocol/iter.rs (2)

110-110: LGTM: Formatting improvement enhances readability.

The blank line in the Deref trait implementation improves code organization.


246-253: LGTM: Appropriate constification of constructor.

Making PyIterIter::new a const fn is correct since it performs simple field initialization that can be evaluated at compile time. This enables better optimization opportunities.

vm/src/protocol/number.rs (2)

21-23: LGTM: Appropriate constification of wrapper method.

Making to_number a const fn is correct since it's a simple wrapper that creates a PyNumber from self. This can be safely evaluated at compile time.


443-445: LGTM: Appropriate constification of accessor method.

Making obj a const fn is correct since it's a simple accessor that returns the wrapped object reference. This can be safely evaluated at compile time.

vm/src/function/buffer.rs (2)

54-60: LGTM: Appropriate constification of accessor methods.

Making len and is_empty const fn for ArgBytesLike is correct since they perform simple field access and arithmetic that can be evaluated at compile time. This enables better optimization opportunities.


106-112: LGTM: Appropriate constification of accessor methods.

Making len and is_empty const fn for ArgMemoryBuffer is correct and maintains consistency with the pattern established for ArgBytesLike. These simple operations can be safely evaluated at compile time.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

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

👍

@youknowone youknowone merged commit f31ebf8 into RustPython:main Jun 22, 2025
11 of 12 checks passed
@ShaharNaveh ShaharNaveh deleted the cleanups-2 branch June 22, 2025 07:10
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