flag: DISALLOW_INSTANTIATION by youknowone · Pull Request #6445 · RustPython/RustPython
Walkthrough
Replaces the Unconstructible trait with a new DISALLOW_INSTANTIATION PyTypeFlags across many pyclass declarations, removes the Unconstructible trait, refines pyclass derive slot/Constructor handling, and updates type initialization and new binding to propagate new-slot behavior.
Changes
| Cohort / File(s) | Summary |
|---|---|
Derive macro crates/derive-impl/src/pyclass.rs |
Normalizes slot identifiers (case-insensitive), updates slot-name error message to allow const fn pointers, removes Constructor/Unconstructible enforcement path and related TODOs. |
Core trait removal crates/vm/src/types/slot.rs |
Removed the Unconstructible trait and its slot_new implementation that returned a type error. |
Type system & class init crates/vm/src/builtins/type.rs, crates/vm/src/class.rs |
new_heap/new_static now take mut slots; added set_new helper; adjusted HEAPTYPE/tp_new propagation. Class init binds __new__ only when a type defines its own new slot (object is special-cased). |
VM pyclasses: iterators, views, builtins crates/vm/src/builtins/... (asyncgenerator.rs, builtin_func.rs, bytearray.rs, bytes.rs, coroutine.rs, descriptor.rs, dict.rs, frame.rs, generator.rs, getset.rs, list.rs, memory.rs, range.rs, set.rs, str.rs, tuple.rs) |
Replaced with(..., Unconstructible, ...) with flags(DISALLOW_INSTANTIATION), with(...); removed impl Unconstructible blocks and Unconstructible imports where applicable. |
Protocol & stdlib pyclasses crates/vm/src/protocol/buffer.rs, crates/stdlib/src/... (array.rs, csv.rs, pystruct.rs, sqlite.rs, unicodedata.rs, zlib.rs, ctypes/field.rs, os.rs) |
Added flags(DISALLOW_INSTANTIATION) to many pyclass declarations (iterators, Reader/Writer, Blob/Statement, Ucd, Compress/Decompress, VecBuffer, PyCField, DirEntry, ScandirIterator, UnpackIterator); removed Unconstructible mixins/imports/impls. |
Manifest Cargo.toml |
(unchanged content summary present in diff metadata) |
Sequence Diagram(s)
sequenceDiagram
autonumber
participant Derive as pyclass derive
participant TypeSys as type creation (new_heap/new_static)
participant ClassInit as class initialization (class.rs)
note right of Derive: Macro resolves pyclass attrs\n(normalize slot names, detect Constructor)
Derive->>TypeSys: emit PyTypeSlots (with/without new slot)
TypeSys->>TypeSys: set_new(slots, base) -- mutate slots,tp_new propagation
TypeSys->>ClassInit: register slots on type
ClassInit->>ClassInit: if own slot_new then bind __new__
ClassInit-->>TypeSys: final type ready with DISALLOW_INSTANTIATION flag
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~60 minutes
- Focus review areas:
- crates/derive-impl/src/pyclass.rs — slot name normalization and Constructor-related logic.
- crates/vm/src/builtins/type.rs — set_new, mutable slots handling, HEAPTYPE/tp_new timing.
- crates/vm/src/class.rs — conditional new binding correctness and object special-case.
- Search for any residual references to
Unconstructibleor assumptions about its slot_new behavior. - Spot-check representative pyclass changes across VM and stdlib for consistent application.
Possibly related PRs
- PyType::py_new to return Self #6398 — Modifies constructor/slot_new/py_new machinery; overlaps with type/slot initialization changes.
- Pyfunction builtins and constructor #5823 — Adjusts Constructor usage in pyclass derive logic; related to derive macro recognition changes.
- Use Unconstructible trait for internal types #6052 — Previously changed sqlite Blob/Statement constructibility; directly touches the same pyclasses updated here.
Suggested reviewers
- ShaharNaveh
Poem
"🐰 I hopped through traits and flags today,
Unconstructible went on its way.
DISALLOW_INSTANTIATION plants a sign,
Slots propagated, and new aligned.
A nimble refactor — carrot cake time! 🥕"
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 60.19% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | ✅ Passed | The title 'flag: DISALLOW_INSTANTIATION' clearly and concisely summarizes the main change: introducing the DISALLOW_INSTANTIATION flag throughout the codebase. |
| Linked Issues check | ✅ Passed | The pull request fully implements the objective from #5286 by adding DISALLOW_INSTANTIATION flag to prevent unintended instantiation of various types across the codebase. |
| Out of Scope Changes check | ✅ Passed | All changes are directly related to the DISALLOW_INSTANTIATION implementation. The systematic replacement of Unconstructible trait with the DISALLOW_INSTANTIATION flag is within scope of the linked objective. |
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
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.
Comment @coderabbitai help to get the list of available commands and usage tips.