Skip to content

Include name and path in ImportError repr - #9247

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:split/importerror-repr-315
Oct 20, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:split/importerror-repr-315

Conversation

@youknowdot

@youknowdot youknowdot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Independent native-only extraction from #8954: include explicitly present name and path attributes in ImportError.__repr__, including explicit None. Preserve positional arguments and the dynamic exception class name, so ModuleNotFoundError inherits the new representation.

Only crates/vm/src/exceptions.rs changes (+30/-2). No Lib refresh, dependency change, or copied regression snippet.

Validation

Tested on main b4a1ad232f8eb68523ce687183ce08958d4b6744 plus this exact native delta:

  • Normal pre-commit hooks, locked/offline VM library check and fresh default-feature executable build passed.
  • Six exact CPython 3.15 ImportErrorTests methods from d220409a13b77de7a6c30abe16ea90db01528edc passed in both the new candidate and pinned CPython: attributes, reset attributes, non-string argument, copy/pickle, repr, and ModuleNotFoundError repr after failed import. The historical RustPython baseline passed the first four and failed precisely the two repr methods.
  • Workspace and separate CAPI no-run gates passed.
  • Workspace and separately scoped CAPI Clippy --all-targets passed without warnings.
  • Workspace lib/bin/integration tests: 1,377 passed, no failures or ignored tests. Separate CAPI: 125 passed, four preexisting ignored pytype tests.
  • Workspace doctests: 24 passed, 18 preexisting ignored; separate CAPI doctests had no cases.

Aggregate native gates used CI's CARGO_PROFILE_DEV_DEBUG=0 and CARGO_PROFILE_TEST_DEBUG=0, one build job, incremental disabled and serial test dispatch. Repository optimization levels, debug assertions and overflow checks were preserved. Workspace excludes wasm, venvlauncher and CAPI; CAPI commands ran separately from its own directory.

Focused Python validation used a source-hash-checked class-only AST harness preserving the canonical method bodies, with -B -S, normal import hooks and a 30-second bound. The full test_exceptions module/release-mode suite was not run; unrelated memory/crash and process fixtures were outside the reviewed execution scope. No test assertions or library sources were changed.

AI assistance: OpenAI Codex prepared the extraction, source review, validation and PR text. The model version is not exposed in this session; the commit discloses that limitation rather than guessing a version.

Summary by CodeRabbit

  • Bug Fixes
    • Improved the representation of import errors by including relevant module name and path details when available.
    • Preserved the standard representation when no additional import details are provided.

Extract the independent Python 3.15 ImportError representation change from the upgrade work. Preserve BaseException positional arguments and include explicitly present name/path attributes for ImportError and ModuleNotFoundError.

AI assistance: OpenAI Codex prepared the extraction and validation. Model version is not exposed in this session.

Assisted-by: Codex:unknown (model version not exposed)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T13:42:15.287943Z 78bdb6f 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.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5b64b8c7-ba85-4eaf-9c9f-94248dd81f75
📥 Commits

Reviewing files that changed from the base of the PR and between b4a1ad2 and 78bdb6f.

📒 Files selected for processing: 1
  • crates/vm/src/exceptions.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

PyImportError now implements Representable. Its representation uses the base exception form when name and path are absent, and includes either or both fields when present.

Changes: PyImportError Representation

Layer / File(s) Summary
Implement PyImportError representation
crates/vm/src/exceptions.rs
PyImportError delegates to PyBaseException when neither field is set. Otherwise, its representation appends the present name and path values. The exception traits import includes Representable.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to 78bdb

The representation change is ready to merge after normal checks; no actionable risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 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: adding name and path to the ImportError representation.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 78bdb6f52e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

}
result.push_str(name);
result.push('=');
result.push_str(&value.repr(vm)?.to_string_lossy());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve surrogates in keyword field reprs

When a name or path object has a legal __repr__ returning a string with an unpaired surrogate (for example '\ud800'), PyImportError::repr_str replaces that surrogate with U+FFFD before constructing the Python string. Python str preserves such code points, so repr(ImportError(name=obj)) is corrupted; append the returned PyStr's WTF-8 data (for example via repr_wtf8) instead of using the lossy UTF-8 conversion.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed: a custom repr returning U+D800 was converted to U+FFFD for name/path. Since #9247 merged while validation was in progress, the correction is now in #9252: use repr_wtf8 and push_wtf8 throughout. The four class/field combinations pass against pinned CPython and the newly built candidate, along with the six canonical ImportError methods and the documented native gates. No Lib or repository test changes.

@youknowone
youknowone merged commit bb97696 into RustPython:main Oct 20, 2026
30 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