Skip to content

Fix tuple subclass preservation in type bases - #8420

Open
moreal wants to merge 3 commits into
RustPython:mainfrom
moreal:fix/fail-types-tuple-subclass-as-bases
Open

Fix tuple subclass preservation in type bases#8420
moreal wants to merge 3 commits into
RustPython:mainfrom
moreal:fix/fail-types-tuple-subclass-as-bases

Conversation

@moreal

@moreal moreal commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

type.__bases__ always synthesized a fresh plain tuple from an internal Vec<PyTypeRef>, so a tuple subclass passed as a base class lost its identity (CPython gh-132176). test_tuple_subclass_as_bases no longer needs @unittest.expectedFailure.

  • PyType.bases is now a typed PyTuple<PyTypeRef> instead of a Vec, so __bases__ returns the stored tuple object directly — preserving a tuple subclass passed via bases=.
  • This required tuple to exist as a real type before any other type (including type/object) is constructed, so bootstrap (init_type_hierarchy) now builds tuple and the canonical empty tuple alongside type/object.
  • The old partially_init! macro bootstrap is replaced with explicit, Miri-safe helpers (init_ref_count, initial_ref, clone_raw_ref, init_inner).

Written with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved runtime handling of class inheritance and method resolution.
    • Added validation to ensure type bases are valid classes.
    • Improved tuple and type initialization during startup.
    • Strengthened support for built-in types, including tuples and weak references.
    • Improved cleanup and cycle handling for type hierarchies.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: 547176e7-0939-4c94-a62a-5a060c18f029

📥 Commits

Reviewing files that changed from the base of the PR and between 48a3a1f and fd9cf58.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_types.py is excluded by !Lib/**
📒 Files selected for processing (6)
  • crates/vm/src/builtins/tuple.rs
  • crates/vm/src/builtins/type.rs
  • crates/vm/src/object/core.rs
  • crates/vm/src/object/ext.rs
  • crates/vm/src/types/zoo.rs
  • crates/vm/src/vm/context.rs

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The change stores PyType.bases as a typed Python tuple. It adds typed tuple construction and updates type validation, traversal, MRO handling, and bootstrap initialization. The type zoo and VM context now share the bootstrapped tuple type and empty tuple.

Changes

Typed tuple bases and bootstrap hierarchy

Layer / File(s) Summary
Typed tuple base representation
crates/vm/src/builtins/tuple.rs, crates/vm/src/builtins/type.rs
Typed tuple references and constructors support storing Python tuples as PyType bases.
Type base operations
crates/vm/src/builtins/type.rs
Type construction, traversal, mutation, MRO handling, and validation now use typed tuple bases.
Core hierarchy bootstrap
crates/vm/src/object/core.rs, crates/vm/src/object/ext.rs
Bootstrap code explicitly initializes the core types and tuples, including atomic references and hierarchy invariants.
Type zoo integration
crates/vm/src/types/zoo.rs, crates/vm/src/vm/context.rs
TypeZoo and Context now use the shared bootstrapped tuple type and empty tuple.

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: ⚪ Minimal · up to fd9cf

This change preserves tuple subclasses passed as type bases, and no actionable merge-blocking risk remains after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant ContextInit
  participant TypeZooInit
  participant InitTypeHierarchy
  participant CoreTypes
  ContextInit->>TypeZooInit: initialize types and empty_tuple
  TypeZooInit->>InitTypeHierarchy: create BootstrapTypeHierarchy
  InitTypeHierarchy->>CoreTypes: allocate and initialize core types and tuples
  InitTypeHierarchy-->>TypeZooInit: return hierarchy and empty_tuple
  TypeZooInit-->>ContextInit: return TypeZoo and empty_tuple
Loading

Suggested reviewers: youknowone

🚥 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 clearly and concisely describes the main change: preserving tuple subclasses in type bases.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
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.
✨ 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.

@github-actions

github-actions Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/types.py
[ ] test: cpython/Lib/test/test_types.py (TODO: 3)

dependencies:

  • types

dependent tests: (57 tests)

  • types: test_annotationlib test_ast test_asyncgen test_asyncio test_builtin test_call test_code test_collections test_compile test_compiler_assemble test_coroutines test_descr test_dis test_doctest test_dtrace test_dynamicclassattribute test_email test_enum test_exception_group test_fstring test_funcattrs test_generators test_genericalias test_global test_hmac test_importlib test_inspect test_listcomps test_marshal test_monitoring test_opcache test_optimizer test_os test_pdb test_positional_only_arg test_pprint test_pyclbr test_pydoc test_raise test_rlcompleter test_string test_subclassinit test_subprocess test_tempfile test_threading test_trace test_traceback test_type_aliases test_type_annotations test_type_params test_types test_typing test_unittest test_userdict test_xml_etree test_xml_etree_c test_xxlimited

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone youknowone added the z-ca-2026 Tag to track Contribution Academy 2026 label Aug 1, 2026
@moreal moreal self-assigned this Aug 2, 2026
@moreal
moreal force-pushed the fix/fail-types-tuple-subclass-as-bases branch from 69415b0 to ff3c599 Compare August 23, 2026 11:52
moreal added 3 commits August 23, 2026 20:54
Store validated base classes in the original typed tuple, while retaining a bootstrap vector only until a Python tuple can be materialized.

Assisted-by: gpt-5.6-sol
Initialize tuple and the canonical empty tuple alongside type and object so every type stores an actual typed bases tuple from construction. Remove the bootstrap bases representation and keep raw initialization Miri-safe.

Assisted-by: Codex:gpt-5.6-sol
@moreal
moreal force-pushed the fix/fail-types-tuple-subclass-as-bases branch from ff3c599 to fd9cf58 Compare August 23, 2026 12:15
@moreal
moreal marked this pull request as ready for review August 23, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants