Skip to content

Update test_types.py to 3.14.5#7912

Merged
youknowone merged 4 commits into
RustPython:mainfrom
ShaharNaveh:update-test-types
May 19, 2026
Merged

Update test_types.py to 3.14.5#7912
youknowone merged 4 commits into
RustPython:mainfrom
ShaharNaveh:update-test-types

Conversation

@ShaharNaveh

@ShaharNaveh ShaharNaveh commented May 18, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes
    • Namespace replacement now validates that the replacement object has the same runtime type as the original. If types differ, the operation raises a TypeError that indicates the expected SimpleNamespace-style type and the actual returned object type.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR adds a runtime type check in PyNamespace::replace to ensure the object returned by calling the original namespace class has the same runtime type as the original object, raising a TypeError with expected and actual type names on mismatch.

Changes

Namespace replace type validation

Layer / File(s) Summary
Runtime type check in __replace__
crates/vm/src/builtins/namespace.rs
PyNamespace::__replace__ now verifies that the object produced by calling the original namespace’s class has the same runtime type as the original namespace; raises a TypeError including the expected SimpleNamespace-style type name and the actual returned object type when they differ.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • youknowone

Poem

🐰 In a burrow of code, neat and tight,
A namespace donned the wrong type at night.
I checked the class, gave types a peep,
Now replacements match — no more leap!
Hooray, tidy hops and a peaceful byte.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title mentions updating 'test_types.py' to version 3.14.5, but the actual changes are to the namespace.rs file implementing type verification in PyNamespace::replace, which is unrelated to test file updates. Update the title to accurately reflect the main change, such as 'Add type verification to PyNamespace::replace' or 'Verify namespace class type in replace method'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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.

@github-actions

github-actions Bot commented May 18, 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: 8)

dependencies:

  • types

dependent tests: (55 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_positional_only_arg test_pprint test_pyclbr test_pydoc test_raise 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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/vm/src/builtins/namespace.rs`:
- Line 60: The error message in __replace__ is using
Self::class(&vm.ctx).slot_name() which always yields the base "SimpleNamespace";
change it to use the runtime object's class name instead (e.g., call
class(&vm.ctx).slot_name() on the instance variable used in __replace__, such as
the `obj`/`value` parameter) so the message reports the actual subclass name at
runtime.
- Around line 61-65: The code currently calls
zelf.class().__qualname__(vm).downcast_ref::<PyStr>().map(|n|
n.as_wtf8()).unwrap(), which can panic if __qualname__ is not a PyStr; change
this to handle the downcast failure safely by falling back to
zelf.class().name(vm) (or otherwise returning an Err/PyErr) instead of
unwrapping: attempt the downcast_ref::<PyStr>(), map to as_wtf8() when present,
and if None use name() (or return a Python exception via the VM) so the VM
raises a proper error rather than panicking.
🪄 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: 3ffd5eda-543b-42ee-ac9c-87503eee6d43

📥 Commits

Reviewing files that changed from the base of the PR and between 883ce9d and d47d1c5.

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

Comment thread crates/vm/src/builtins/namespace.rs
Comment thread crates/vm/src/builtins/namespace.rs Outdated
@youknowone

Copy link
Copy Markdown
Member

coderabbits comments seems worth to check

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
crates/vm/src/builtins/namespace.rs (1)

60-60: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Use the runtime class name instead of the base class name in the error message.

Line 60 uses Self::class(&vm.ctx).slot_name(), which always returns "SimpleNamespace" regardless of the actual runtime type. If __replace__ is called on a subclass instance, the error message will incorrectly state it expected SimpleNamespace when it should reference the subclass name.

🔧 Proposed fix
             return Err(vm.new_type_error(format!(
                 "expect {} type, but {}() returned '{}' object",
-                Self::class(&vm.ctx).slot_name(),
+                zelf.class().slot_name(),
                 zelf.class()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/vm/src/builtins/namespace.rs` at line 60, The error message in the
__replace__ implementation uses Self::class(&vm.ctx).slot_name(), which always
yields the base class name ("SimpleNamespace") instead of the actual runtime
type; change the code to fetch the instance's runtime class name (e.g., use
value.class(&vm.ctx).slot_name() or the equivalent method on the instance being
checked) and include that runtime name in the process error/TypeError message so
subclasses are reported correctly; update the __replace__ error branch in
namespace.rs to reference the instance's class name rather than
Self::class(&vm.ctx).slot_name().
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@crates/vm/src/builtins/namespace.rs`:
- Line 60: The error message in the __replace__ implementation uses
Self::class(&vm.ctx).slot_name(), which always yields the base class name
("SimpleNamespace") instead of the actual runtime type; change the code to fetch
the instance's runtime class name (e.g., use value.class(&vm.ctx).slot_name() or
the equivalent method on the instance being checked) and include that runtime
name in the process error/TypeError message so subclasses are reported
correctly; update the __replace__ error branch in namespace.rs to reference the
instance's class name rather than Self::class(&vm.ctx).slot_name().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: f77484af-ecd2-46e0-886d-495e0f4b76e2

📥 Commits

Reviewing files that changed from the base of the PR and between d47d1c5 and 03a1c73.

📒 Files selected for processing (1)
  • crates/vm/src/builtins/namespace.rs

@ShaharNaveh ShaharNaveh requested a review from youknowone May 19, 2026 07:03
@youknowone youknowone enabled auto-merge (squash) May 19, 2026 07:04
@youknowone youknowone merged commit e8d7437 into RustPython:main May 19, 2026
27 checks passed
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