Skip to content

[Python] Fix type var scoping for PEP 695 annotations#4362

Merged
dbrattli merged 3 commits intomainfrom
dbrattli/python-type-fixes
Feb 25, 2026
Merged

[Python] Fix type var scoping for PEP 695 annotations#4362
dbrattli merged 3 commits intomainfrom
dbrattli/python-type-fixes

Conversation

@dbrattli
Copy link
Collaborator

Summary

  • 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)

Test plan

  • Run ./build.sh test python to verify no regressions
  • Run pyright CI check to confirm test_mailbox_processor.py passes

🤖 Generated with Claude Code

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>
@github-actions
Copy link

github-actions bot commented Feb 25, 2026

Python Type Checking Results (Pyright)

Metric Value
Total errors 18
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_applicative.py 12 Excluded
temp/tests/Python/test_hash_set.py 3 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 1 Excluded

dbrattli and others added 2 commits February 25, 2026 18:40
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>
@dbrattli dbrattli merged commit ed16114 into main Feb 25, 2026
44 of 46 checks passed
@dbrattli dbrattli deleted the dbrattli/python-type-fixes branch February 25, 2026 21:14
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.

1 participant