Repository navigation
Refuse variable inlining across deferred annotation boundaries - #894
Open
yangfan-yf-yf wants to merge 1 commit into
Open
yangfan-yf-yf wants to merge 1 commit into
yangfan-yf-yf wants to merge 1 commit 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 26819 27073 +254
==========================================
+ Hits 25570 25828 +258
+ Misses 1249 1245 -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
The tests execute the original and retained/transformed programs in subprocesses, compare their observable results, and verify that a refusal leaves the project files unchanged. They cover rebinding, side-effect timing, parameter/return/module/class annotations, qualified and aliased imports, default
get_type_hintsnamespace behavior, partial refactoring, and scope/eager controls. After the full-suite run, the resources positive control was narrowed to a tuple supported by the existing API; the affected tests passed on both versions with no further product changes.