Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Jan 20, 2026

the _ prefixed name is related to #3284

Summary by CodeRabbit

Release Notes

  • New Features

    • Added Capsule type for Python capsule objects.
    • Introduced _types module providing access to built-in type objects including AsyncGeneratorType, BuiltinFunctionType, BuiltinMethodType, CapsuleType, CodeType, FunctionType, GeneratorType, MethodType, ModuleType, and others.
  • Refactor

    • Unified native function and bound method types under a consolidated representation.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 20, 2026

Warning

Rate limit exceeded

@youknowone has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 14 minutes and 38 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

The changes refactor the builtin function and method type hierarchy to unify PyNativeFunction and PyNativeMethod under a shared "builtin_function_or_method" type with C-compatible representation, introduce a new PyCapsule type for capsule objects, and add a centralized _types module exposing all built-in Python type objects as accessor functions.

Changes

Cohort / File(s) Summary
Native Function & Method Type Unification
crates/vm/src/builtins/builtin_func.rs, crates/vm/src/function/method.rs
PyNativeFunction now includes #[repr(C)] annotation and expanded fields (value, zelf, module). PyNativeMethod inherits from PyNativeFunction and shares the builtin_function_or_method type. Introduces NativeFunctionOrMethod wrapper for common data access. Method building updated to use the unified type instead of distinct builtin_method_type.
Capsule Type Implementation
crates/vm/src/builtins/capsule.rs, crates/vm/src/builtins/mod.rs
New PyCapsule struct added with Representable and PyPayload trait implementations, producing <capsule object> repr. Module declaration and re-export added to builtins.
Built-in Type Exposure
crates/vm/src/stdlib/_types.rs, crates/vm/src/stdlib/mod.rs
New _types module created with 23+ accessor functions (AsyncGeneratorType, BuiltinFunctionType, CapsuleType, etc.) returning Python type objects from vm.ctx.types. Module integrated into builtin_module_defs initialization.
Type Registry Updates
crates/vm/src/types/zoo.rs
TypeZoo struct gains new capsule_type field. Capsule type initialized via PyCapsule::init_builtin_type(). builtin_function_or_method_type now reuses same type instance as builtin_method_type. Capsule initialization invoked in extension flow.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • arihant2math
  • coolreader18

Poem

A rabbit hops through builtin lands,
Native functions now join paws and hands,
Capsules bundled, types on display,
_types module lights the way,
One grand type zoo, hip hooray! 🐰✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add _types module' accurately summarizes the main change: introducing a new _types module with type accessors, though it omits supporting infrastructure changes like PyCapsule and builtin_function_or_method consolidation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@youknowone youknowone changed the title _types Add _types module Jan 24, 2026
@youknowone youknowone marked this pull request as ready for review January 24, 2026 13:08
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

🤖 Fix all issues with AI agents
In `@crates/vm/src/stdlib/_types.rs`:
- Around line 107-111: The ClassMethodDescriptorType function currently returns
vm.ctx.types.method_descriptor_type, causing types.ClassMethodDescriptorType to
be identical to MethodDescriptorType; instead either register and return a
distinct type object for class method descriptors (e.g. create and expose a new
vm.ctx.types.class_method_descriptor_type via the same type-registration flow
used for other descriptor types and return that from ClassMethodDescriptorType)
or remove/withhold the pyattr until the dedicated type is implemented; update
the ClassMethodDescriptorType implementation to return the new unique type
symbol (class_method_descriptor_type) rather than method_descriptor_type so type
identity matches CPython.
🧹 Nitpick comments (2)
crates/vm/src/builtins/capsule.rs (1)

10-16: Consider simplifying the struct definition.

Since PyCapsule only contains a unit field _private: (), the Clone and Copy derives are technically fine but may be unnecessary. If capsule functionality expands in the future to hold actual data (e.g., raw pointers for FFI), these derives would need reconsideration.

♻️ Optional simplification
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug)]
 pub struct PyCapsule {
     _private: (),
 }
crates/vm/src/builtins/builtin_func.rs (1)

233-244: Consider adding a SAFETY comment to the unsafe block.

The downcast_unchecked relies on the #[repr(C)] layout invariant established in the struct definitions. Adding an explicit // SAFETY: comment would help future maintainers understand why this cast is valid.

📝 Suggested documentation
         if obj.fast_isinstance(class) {
-            // Both PyNativeFunction and PyNativeMethod share the same type now.
-            // PyNativeMethod has `func: PyNativeFunction` as its first field,
-            // so we can safely treat the data pointer as PyNativeFunction for reading.
+            // SAFETY: Both PyNativeFunction and PyNativeMethod share the same Python type.
+            // PyNativeMethod has `func: PyNativeFunction` as its first field with #[repr(C)],
+            // guaranteeing func is at offset 0. This allows safely reading PyNativeFunction
+            // fields from either type.
             Ok(Self(unsafe { obj.downcast_unchecked() }))

Comment on lines +107 to +111
#[pyattr]
fn ClassMethodDescriptorType(vm: &VirtualMachine) -> PyObjectRef {
// TODO: implement as separate type
vm.ctx.types.method_descriptor_type.to_owned().into()
}
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 | 🟠 Major

ClassMethodDescriptorType should not alias MethodDescriptorType.

On Line 109, this TODO means types.ClassMethodDescriptorType becomes identical to types.MethodDescriptorType, which diverges from CPython and can break code that relies on type identity checks. Please introduce a dedicated type (or avoid exposing this attribute until it exists).

🤖 Prompt for AI Agents
In `@crates/vm/src/stdlib/_types.rs` around lines 107 - 111, The
ClassMethodDescriptorType function currently returns
vm.ctx.types.method_descriptor_type, causing types.ClassMethodDescriptorType to
be identical to MethodDescriptorType; instead either register and return a
distinct type object for class method descriptors (e.g. create and expose a new
vm.ctx.types.class_method_descriptor_type via the same type-registration flow
used for other descriptor types and return that from ClassMethodDescriptorType)
or remove/withhold the pyattr until the dedicated type is implemented; update
the ClassMethodDescriptorType implementation to return the new unique type
symbol (class_method_descriptor_type) rather than method_descriptor_type so type
identity matches CPython.

@github-actions
Copy link
Contributor

Code has been automatically formatted

The code in this PR has been formatted using:

  • cargo fmt --all
    Please pull the latest changes before pushing again:
git pull origin _types

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