Repository navigation
Refuse variable inlining across deferred annotation boundaries - #894
Open
yangfan-yf-yf wants to merge 2 commits into
Open
yangfan-yf-yf wants to merge 2 commits into
yangfan-yf-yf wants to merge 2 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #894 +/- ##
==========================================
+ Coverage 95.34% 95.40% +0.05%
==========================================
Files 134 135 +1
Lines 26762 27016 +254
==========================================
+ Hits 25516 25774 +258
+ Misses 1246 1242 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
Description
Inlining
target = originalintodef func(x: target)can changetyping.get_type_hints(func)["x"]frominttostrwhenoriginalis rebound after the function definition. The same substitution can defer side effects until annotations are accessed. This occurs withfrom __future__ import annotationsand with default deferred annotations on Python 3.14+.I added a conservative preflight check that refuses variable inlining across these deferred annotation boundaries. It retains the original binding and source rather than moving an initializer into a different evaluation context. The check handles parameter and return annotations and simple module/class variable annotations; local variable annotations, non-simple annotated assignments, function defaults, and eager annotation controls remain available for ordinary inlining. Keeping the binding with
only_current=True, remove=Falsealso permits an eager reference to be inlined.When a non-local binding would be removed, the check also scans Python files outside the requested write scope. For stringified annotations, it checks name and attribute expressions against their module namespace, including imported aliases, because default
get_type_hintsmay use module globals even when static name lookup finds a class member or parameter.Explicit string-literal forward references such as
x: "target"remain outside this change. This does not replace the separate type-alias handling in #890 or promise preservation for arbitrary caller-provided namespaces.Checklist
Validation
2bd17a8(tested on 2026-10-06), the 64-case regression file had 22 failures, 12 passes, and 30 skips on Python 3.12.3; on Python 3.14.7 it had 40 failures, 22 passes, and 2 skips.22f5e501. Python 3.12.3 full suite: 2,183 passed, 42 skipped, 5 xfailed. Python 3.14.7 new regressions plus the existing inline suite: 164 passed, 2 skipped. All applicable new cases pass.The regression programs run in subprocesses, check observable results, and verify that a refusal leaves project files unchanged. Eager controls and partial inlining with the original binding retained continue to execute successfully. The guard itself is unchanged from the original patch; the changelog includes the current master entries, and multiline test source samples use
textwrap.dedent()under the latest contribution guideline.