X Tutup
Skip to content

type.__new__: preserve caller namespace when reading __qualname__#7524

Merged
youknowone merged 2 commits intoRustPython:mainfrom
wheevu:fix-type-qualname-ns-mutation
Mar 28, 2026
Merged

type.__new__: preserve caller namespace when reading __qualname__#7524
youknowone merged 2 commits intoRustPython:mainfrom
wheevu:fix-type-qualname-ns-mutation

Conversation

@wheevu
Copy link
Copy Markdown
Contributor

@wheevu wheevu commented Mar 28, 2026

type(name, bases, ns) currently mutates the caller-provided namespace by popping __qualname__.

This changes PyType::slot_new to read __qualname__ without mutating the input namespace, then remove it from the copied attributes. That keeps the caller namespace while still keeping __qualname__ out of the created type’s __dict__, which matches CPython.

This also removes the @unittest.expectedFailure marker from test_qualname_dict.

Tested with:

  • cargo run --release -- -m unittest test.test_descr.ClassPropertiesAndMethods.test_qualname_dict
  • cargo run --release -- -c "ns={'__qualname__':'some.name'}; tp=type('Foo',(),ns); print(tp.__qualname__); print('__qualname__' in tp.__dict__); print(ns)"
  • cargo run --release -- -c "ns={1: 2, '__qualname__': 1}; type('Foo', (), ns)"
  • cargo fmt
  • cargo clippy

Summary by CodeRabbit

  • Refactor
    • Internal improvement to type-construction logic: metadata lookup is now separated from attribute extraction, and metadata is removed explicitly from attributes. No changes to public behavior, APIs, or user-facing functionality.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 28, 2026

📦 Library Dependencies

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

[ ] test: cpython/Lib/test/test_descr.py (TODO: 38)
[ ] test: cpython/Lib/test/test_descrtut.py (TODO: 3)

dependencies:

dependent tests: (no tests depend on descr)

Legend:

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 28, 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: 97bbeafd-2139-456f-afd8-4f19997178ab

📥 Commits

Reviewing files that changed from the base of the PR and between f1c1470 and 5bcc7b7.

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

📝 Walkthrough

Walkthrough

Refactors PyType::slot_new to stop popping __qualname__ from the incoming dict; instead it first reads __qualname__ via get_item_opt(...), converts the dict to an attributes map, then removes __qualname__ from that map with attributes.shift_remove(...).

Changes

Cohort / File(s) Summary
PyType Constructor Refactor
crates/vm/src/builtins/type.rs
Changed __qualname__ handling in PyType::slot_new: lookup with get_item_opt(...) before converting the dict to attributes, then remove __qualname__ via attributes.shift_remove(...) instead of popping from the original dict.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • #6010: Concurrently modifies PyType constructor in the same file, affecting attribute and dict descriptor handling.

Poem

🐰 I peeked the qualname, gentle and sly,

I turned the dict to attributes by and by,
Then plucked the name with a careful shove,
No double-removal, just tidy love,
Hoppity code, neat as a glove!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'type.new: preserve caller namespace when reading qualname' directly describes the main change in the pull request, which is modifying how PyType::slot_new handles qualname to avoid mutating the caller-provided namespace.

✏️ 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
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/vm/src/builtins/type.rs`:
- Around line 1870-1873: The code calls dict.to_attributes(vm) before extracting
__qualname__, which can force early iteration/validation and panic on non-string
keys; instead, remove and validate the __qualname__ entry from the original dict
prior to converting to attributes. Concretely: call
dict.shift_remove(identifier!(vm, __qualname__)) (or an equivalent safe lookup)
on the incoming dict first and run downcast_qualname on that result, then call
dict.to_attributes(vm) to build attributes; reference symbols:
dict.to_attributes(vm), identifier!(vm, __qualname__), shift_remove,
downcast_qualname, and the type.__new__ namespace handling where this ordering
occurs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 10c0b0b5-7c9d-4591-ac15-a32d110b1b27

📥 Commits

Reviewing files that changed from the base of the PR and between 1a9b10e and f1c1470.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_descr.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/builtins/type.rs

Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgmt!
ty, and welcome to the project:)

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!

@youknowone youknowone merged commit da440db into RustPython:main Mar 28, 2026
16 checks passed
@wheevu wheevu deleted the fix-type-qualname-ns-mutation branch March 28, 2026 15:54
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.

3 participants

X Tutup