Skip to content

Downcastable #5986

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 3 commits into from
Jul 16, 2025
Merged

Downcastable #5986

merged 3 commits into from
Jul 16, 2025

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jul 16, 2025

Summary by CodeRabbit

  • Refactor

    • Unified and modernized type downcasting throughout the codebase, replacing older payload-based methods with safer downcast-based APIs for Python object type checks and conversions.
    • Deprecated legacy payload accessors in favor of new downcast methods, improving code consistency and type safety.
    • Updated several internal method signatures to remove unnecessary parameters and streamline type operations.
  • Style

    • Added "downcastable" to the spell checker dictionary to prevent false positives in spell checking.

Copy link
Contributor

coderabbitai bot commented Jul 16, 2025

Walkthrough

This change refactors the type downcasting and type-checking mechanisms throughout the codebase, replacing previous payload, payload_is, and related methods with new downcast_ref and downcastable methods. Deprecated methods are marked accordingly, and macro and trait usages are updated for consistency. Several public method signatures are also updated to remove unnecessary parameters.

Changes

File(s) Change Summary
.cspell.json Added "downcastable" to the spellchecker whitelist.
stdlib/src/array.rs, stdlib/src/csv.rs, stdlib/src/select.rs, stdlib/src/sqlite.rs, stdlib/src/termios.rs Replaced payload and payload_is with downcast_ref and downcastable for type checking and downcasting.
vm/src/builtins/asyncgenerator.rs, vm/src/builtins/bool.rs, vm/src/builtins/builtin_func.rs, vm/src/builtins/complex.rs, vm/src/builtins/float.rs, vm/src/builtins/int.rs, vm/src/builtins/iter.rs, vm/src/builtins/list.rs, vm/src/builtins/mappingproxy.rs, vm/src/builtins/memory.rs, vm/src/builtins/range.rs, vm/src/builtins/set.rs, vm/src/builtins/str.rs, vm/src/builtins/type.rs, vm/src/builtins/union.rs Updated all type checks and downcasts to use downcast_ref, downcast_ref_if_exact, or downcastable; removed or updated deprecated payload-related method calls.
vm/src/cformat.rs, vm/src/codecs.rs, vm/src/convert/transmute_from.rs, vm/src/convert/try_from.rs, vm/src/dict_inner.rs, vm/src/exceptions.rs, vm/src/frame.rs, vm/src/macros.rs, vm/src/object/core.rs, vm/src/protocol/number.rs, vm/src/protocol/object.rs, vm/src/protocol/sequence.rs, vm/src/py_serde.rs, vm/src/sliceable.rs, vm/src/stdlib/collections.rs, vm/src/stdlib/ctypes/array.rs, vm/src/stdlib/ctypes/function.rs, vm/src/stdlib/io.rs, vm/src/stdlib/itertools.rs, vm/src/stdlib/operator.rs, vm/src/stdlib/os.rs, vm/src/stdlib/sre.rs, vm/src/stdlib/weakref.rs, vm/src/types/slot.rs, vm/src/vm/interpreter.rs, vm/src/vm/mod.rs, vm/src/vm/vm_ops.rs, wasm/lib/src/convert.rs Systematic replacement of payload, payload_is, and related subclass/exact checks with downcast_ref, downcast_ref_if_exact, and downcastable methods; macro and trait implementations updated for new API.
vm/src/object/core.rs Major refactor: Introduced downcastable and new downcast_ref methods on PyObject, deprecated old payload-based methods, updated assertions and macro usages.
vm/src/builtins/int.rs Multiple public method signatures updated to remove unnecessary vm parameter; now use downcast_ref for type checks.

Sequence Diagram(s)

sequenceDiagram
    participant PyObject
    participant ConsumerFunction

    ConsumerFunction->>PyObject: downcast_ref::<T>()
    alt Type matches
        PyObject-->>ConsumerFunction: Some(&Py<T>)
    else Type does not match
        PyObject-->>ConsumerFunction: None
    end
Loading
sequenceDiagram
    participant PyObject
    participant ConsumerFunction

    ConsumerFunction->>PyObject: downcastable::<T>()
    alt Type matches
        PyObject-->>ConsumerFunction: true
    else Type does not match
        PyObject-->>ConsumerFunction: false
    end
Loading

Poem

Down with old payloads, up with the new!
Downcastable rabbits now know what to do.
With safer checks and methods refactored,
Our code is less brittle, our logic less fractured.
🐇 A hop, a skip, and a type-safe cheer—
Downcasting is clearer, the intent is sincere!

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

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.

@youknowone youknowone marked this pull request as ready for review July 16, 2025 07:44
@youknowone youknowone merged commit 6a3dff6 into RustPython:main Jul 16, 2025
12 checks passed
@youknowone youknowone deleted the downcastable branch July 16, 2025 09:00
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.

1 participant