[Python] Fix type var scoping for PEP 695 annotations#4362
Merged
Conversation
Prevent generic type params from leaking outside their function scope: - Emit `Any` for type vars outside function type param scope (e.g., class fields, variable annotations) instead of referencing undefined type vars - Only add actually-declared (repeated) generic params to ScopedTypeParams, so non-repeated params erased to Any don't leak - Remove test_mailbox_processor.py from pyright CI exclude list (now passes) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Python Type Checking Results (Pyright)
Excluded files with errors (4 files)These files have known type errors and are excluded from CI. Remove from
|
Revert to using getGenericTypeParams for ScopedTypeParams so that all
outer function type params are tracked, preventing inner functions from
re-declaring them in PEP 695 syntax ("TypeVar X is already in use by
an outer scope"). The annotation change (emit Any outside function
scope) is kept as that was the actual fix for the mailbox_processor issue.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Two-part fix for PEP 695 type var scoping: 1. In transformFunction, only add repeated (actually-declared) generic params to ScopedTypeParams. Non-repeated params erased to Any must not enter scope, or inner functions will emit undefined type var references (e.g., Async[_A] where _A is never declared). 2. In transformModuleFunction, compute the declared type params (explicit generics + signature generics) BEFORE transforming the body and add them to ScopedTypeParams. This prevents inner functions from re-declaring type vars that the outer function already declares in its PEP 695 [] syntax. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Anyfor type vars outside function type param scope (e.g., class fields, variable annotations) instead of referencing undefined type varsScopedTypeParams, so non-repeated params erased toAnydon't leaktest_mailbox_processor.pyfrom pyright CI exclude list (now passes)Test plan
./build.sh test pythonto verify no regressionstest_mailbox_processor.pypasses🤖 Generated with Claude Code