Skip to content

Fix __qualname__ of compiler-generated __annotate__ functions - #8498

Draft
kyokuping wants to merge 1 commit into
RustPython:mainfrom
kyokuping:annotate-qualname
Draft

Fix __qualname__ of compiler-generated __annotate__ functions#8498
kyokuping wants to merge 1 commit into
RustPython:mainfrom
kyokuping:annotate-qualname

Conversation

@kyokuping

@kyokuping kyokuping commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Assisted-by: Codex:5.6-sol

Summary

Under PEP 649, Python creates a generated __annotate__ function at compile time. This function holds the deferred annotation expressions for a function or class. Its __qualname__ should match the qualified name of the annotated target, and __code__.co_qualname should match it too.

In CPython, a similar issue was reported as python/cpython#137814 and resolved in PR python/cpython#137842.

In RustPython's codegen, annotation expression scopes were compiled in the enclosing scope, but the generated code object omitted the annotated target's own name when building the scope's qualified name. This caused both __qualname__ and __code__.co_qualname on the generated __annotate__ function to be incorrect.

To resolve this, the codegen phase where the annotation expression scope is constructed has been modified so that the annotated target's name is included when forming the scope's qualified name. This keeps __qualname__ on the generated __annotate__ function aligned with the annotated target and ensures __code__.co_qualname remains consistent with CPython.

As a result, RegressionTests.test_annotate_qualname in Lib/test/test_type_annotations.py is no longer marked as expectedFailure.

Summary by CodeRabbit

  • Bug Fixes
    • Improved qualified-name generation for functions using annotations.
    • Annotation scopes now include the associated function name, producing more accurate names in generated output and tooling.
    • Existing qualified-name behavior remains unchanged for standard code objects.

@coderabbitai

coderabbitai Bot commented Aug 11, 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: 106c046d-848b-4163-8009-ae1ff0a7b7ae

📥 Commits

Reviewing files that changed from the base of the PR and between 81df1ff and 31b9efe.

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

📝 Walkthrough

Walkthrough

The compiler now passes annotated function names into annotation scopes. Qualified-name generation uses optional function names and preserves standard behavior for other scopes.

Changes

Annotation Scope Qualified Names

Layer / File(s) Summary
Pass the annotated function name
crates/codegen/src/compile.rs
enter_annotation_scope accepts the annotated function name and reapplies the annotation-scope qualified name after entering the scope.
Build qualified names
crates/codegen/src/compile.rs
Qualname assignment supports optional function names. Construction builds a parent-based prefix and inserts the annotated function name when required.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers: shaharnaveh, 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 identifies the main change: correcting qualified names for compiler-generated annotate functions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

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

[ ] lib: cpython/Lib/typing.py
[ ] test: cpython/Lib/test/test_typing.py (TODO: 2)
[x] test: cpython/Lib/test/test_type_aliases.py
[x] test: cpython/Lib/test/test_type_annotations.py
[ ] test: cpython/Lib/test/test_type_params.py (TODO: 1)
[x] test: cpython/Lib/test/test_genericalias.py

dependencies:

  • typing (native: _typing, collections.abc, sys)
    • collections (native: _collections, _weakref, itertools, sys)
    • inspect (native: builtins, collections.abc, importlib.machinery, itertools, sys)
    • warnings (native: _contextvars, _thread, _warnings, builtins, sys)
    • abc, annotationlib, contextlib, copyreg, functools, operator, re, types

dependent tests: (19 tests)

  • typing: test_annotationlib test_builtin test_copy test_enum test_fractions test_funcattrs test_functools test_genericalias test_grammar test_inspect test_isinstance test_patma test_peg_generator test_pydoc test_pyrepl test_type_aliases test_type_params test_types test_typing

Legend:

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

@kyokuping
kyokuping marked this pull request as ready for review August 11, 2026 17:20
@kyokuping
kyokuping marked this pull request as draft August 11, 2026 17:22
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.

PEP 649: generated __annotate__ functions have incorrect qualified names

1 participant