Skip to content

Add macro for type check functions in c-api#7871

Merged
youknowone merged 2 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-type-check
May 17, 2026
Merged

Add macro for type check functions in c-api#7871
youknowone merged 2 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-type-check

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Contributor

@bschoenmaeckers bschoenmaeckers commented May 13, 2026

Summary by CodeRabbit

  • New Features
    • Added exported C API functions providing runtime type checks (exact-type and subtype checks) for extension modules.
    • Introduced a C-accessible subtype query to inspect type relationships between runtime types.
    • Expanded interoperability surface for extensions performing type inspections and comparisons.

Review Change Stack

@bschoenmaeckers bschoenmaeckers changed the title Add macro for type check function in c-api Add macro for type check functions in c-api May 13, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: c7fad624-873f-4cf8-8222-9dd4250cc2d6

📥 Commits

Reviewing files that changed from the base of the PR and between 69b9f04 and cc1455b.

📒 Files selected for processing (1)
  • crates/capi/src/object.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/capi/src/object.rs

📝 Walkthrough

Walkthrough

Adds a macro-generated pair of C-exported functions PyType_Check (subtype) and PyType_CheckExact (exact match), plus PyType_IsSubtype(a, b); all run their checks inside with_vm and return results as c_int.

Changes

Type-checking C API exports

Layer / File(s) Summary
Type-checking and type relationship functions
crates/capi/src/object.rs
define_py_check! macro defines PyType_Check and PyType_CheckExact to run subtype/exact checks against vm.ctx.types.type_type inside with_vm; PyType_IsSubtype dereferences *const PyTypeObject inputs, calls a.is_subtype(b) inside with_vm, and returns a c_int result.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant with_vm
  participant vm_ctx_types_type_type
  participant PyTypeObject
  Caller->>with_vm: call PyType_Check(obj) / PyType_CheckExact(obj)
  with_vm->>vm_ctx_types_type_type: compare obj.type against ctx.types.type_type
  Caller->>with_vm: call PyType_IsSubtype(a, b)
  with_vm->>PyTypeObject: call a.is_subtype(b)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • youknowone

Poem

🐰 I hopped through vm's cozy den,
Peeked at types and then again,
Exact or subtype — a tiny quest,
Returned a number, passed the test,
Hooray, C calls now know the rest!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a macro for type check functions in the C API, which aligns with the introduction of define_py_check! macro and the three exported type-checking functions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown
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.

👍

Comment thread crates/capi/src/object.rs Outdated
@bschoenmaeckers
Copy link
Copy Markdown
Contributor Author

@youknowone This is also ready to be merged!

Copy link
Copy Markdown
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.

Thank you for reminding!

@youknowone youknowone enabled auto-merge (squash) May 17, 2026 11:53
@youknowone youknowone merged commit a1a87dc into RustPython:main May 17, 2026
26 checks passed
@bschoenmaeckers bschoenmaeckers deleted the c-api-type-check branch May 18, 2026 15:51
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