GeezerVison is showing github.com — Decide tp_new inheritance of static types at type initialization by youknowone · Pull Request #9386 · RustPython/RustPython
Conversation
youknowone
commented
Oct 26, 2026
•
edited by coderabbitai
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
A static type that defines no tp_new and whose base is object used to inherit object.__new__. Calling such a type created an instance whose class is the type but whose payload is a plain object:
>>> type(iter(lambda: 0, 1))() # before: a callable_iterator with no iterator state
>>> import re; re.Pattern() # before: succeeds
>>> import collections; type(collections.OrderedDict().keys())()
The reference interpreter rejects all of these with TypeError: cannot create '...' instances (type_ready_set_new). This PR makes PyType::set_new follow the same rule:
- A static type with no
tp_new of its own whose base is object gets DISALLOW_INSTANTIATION and keeps tp_new unset. Such types no longer expose an inherited __new__ in their dict.
- Other types inherit the base's
tp_new as before. A static type that inherits a non-NULL tp_new must have the same basicsize as that base, because the inherited function allocates the base payload; type initialization panics otherwise. Types flagged HEAPTYPE are exempt, like classes created at runtime.
Types that the reference interpreter does allow to be instantiated keep working:
_io._IOBase is a heap type there and inherits object.__new__; it is flagged HEAPTYPE here so it keeps doing so (object.__new__(C) for class C(_io._IOBase) still passes the staticbase check).
_asyncio.FutureIter, TaskStepMethWrapper, _testinternalcapi hamt, InstructionSequence and itertools._grouper get constructors of their own, matching what the reference interpreter accepts.
Test
cargo fmt --all -- --check, cargo clippy --workspace --all-targets --features threading ... -D warnings, cargo test --workspace --features threading ..., (cd crates/capi && cargo test && cargo clippy --all-targets -- -D warnings), (cd crates/wasm && cargo check --target wasm32-unknown-unknown): pass.
cargo run --release --features threading -- -m test on test_io, test_itertools, test_asyncio, test_descr, test_types, test_class, test_typing, test_collections, test_ordered_dict, test_re, test_exceptions, test_weakref, test_ctypes, test_inspect, test_builtin, test_functools, test_pickle, test_copy, test_pydoc, test_abc, test_dis, test_code, test_contextlib, test_unittest: all pass (2 Windows-only asyncio files skipped).
- wasm32-wasip1
wasm-release build with the CI feature set: all 187 extra_tests/snippets that the wasm-wasi job runs pass under wasmer.
- The expressions above, plus
_io._IOBase(), type(len)(), type(iter([]))() and constructing itertools._grouper, print the same results on this build and on CPython 3.14.
Not covered: test_sqlite3 (no _sqlite3 in my build), Linux/Windows-only code paths.
🤖 Generated with Claude Code
Summary by CodeRabbit
- Bug Fixes
- Improved class instantiation behavior, including inherited constructors and types that cannot be instantiated.
- New Features
- Added support for creating default instances of additional built-in objects.
- Improved construction of iterator-grouping objects by accepting their parent iterator and target key.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Codex Review Summary
This comment shows the latest Codex review activity on this pull request.
| Review |
Status |
Commit |
Review trigger |
| 📝 Code Review |
⚠️ Failed 2026-10-26T06:25:16.663549Z We couldn’t complete this review. Comment @codex review to try again. |
050ca71 |
PR opened |
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review" or "@codex security review".
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
coderabbitai
Bot
commented
Oct 26, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|

No actionable comments were generated in the recent review.
Recent review info
-
Run configuration
-
Commits
-
Reviewing files that changed from the base of the PR and between 050ca71 and 81d6061.
Files selected for processing: 1
-
crates/vm/src/builtins/type.rs
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Walkthrough
-
Type slot initialization now distinguishes static and heap types. Several built-in classes gain constructors, including a parameterized constructor for PyItertoolsGrouper.
Changes: Type and constructor changes
| Layer / File(s) |
Summary |
Type allocation and __new__ slot handling crates/vm/src/builtins/type.rs, crates/vm/src/class.rs, crates/vm/src/stdlib/_io.rs |
Type slot initialization distinguishes static and heap types. Static types with a non-null inherited tp_new panic when their basicsize differs from the base. Class extension omits __new__ when instantiation is disallowed. _IOBase gains the HEAPTYPE flag. |
Default constructors for asyncio and test classes crates/stdlib/src/_asyncio.rs, crates/vm/src/stdlib/_testinternalcapi.rs |
PyFutureIter and TaskStepMethWrapper gain no-argument constructors. Hamt and PyInstructionSequence gain constructors that return default or empty instances. |
Parameterized itertools grouper constructor crates/vm/src/stdlib/itertools.rs |
PyItertoolsGrouper gains a constructor that accepts its parent PyItertoolsGroupBy object and target key. |
Priority: Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Suggested reviewers: shaharnaveh, youknowdot
Merge Risk: Low · up to 81d60
-
Directly constructed groupers cannot yield items. This narrow constructor issue remains open, but the change is otherwise mergeable with owner awareness.
Pre-merge checks: 4 · 1
-
Failed checks: 1 warning
| Check name |
Status |
Explanation |
Resolution |
| Docstring Coverage |
 |
Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 6 files. |
Write docstrings for the functions missing them to satisfy the coverage threshold. |
Passed checks: 4 passed
-
| Check name |
Status |
Explanation |
| Description Check |
 |
Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check |
 |
The title clearly and concisely describes the main change: determining tp_new inheritance for static types during type initialization. |
| Linked Issues check |
 |
Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check |
 |
Check skipped because no linked issues were found for this pull request. |
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
A static type with no tp_new of its own whose base is object now gets
DISALLOW_INSTANTIATION instead of inheriting object's tp_new, following
type_ready_set_new. Such types no longer expose an inherited __new__.
A static type that inherits a non-NULL tp_new from its base must have the
same basicsize as that base; type initialization panics otherwise. Types
flagged HEAPTYPE are exempt, like classes created at runtime.
_IOBase is flagged HEAPTYPE so that it keeps inheriting object's tp_new.
FutureIter, TaskStepMethWrapper, hamt, InstructionSequence and
itertools._grouper get constructors of their own.
Assisted-by: Claude:claude-opus-5-5
Assisted-by: Grok:grok-4.7
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|