Skip to content

Refuse variable inlining across lazy type alias boundaries - #890

Open
yangfan-yf-yf wants to merge 2 commits into
python-rope:masterfrom
yangfan-yf-yf:fix/inline-lazy-type-alias
Open

yangfan-yf-yf wants to merge 2 commits into
python-rope:masterfrom
yangfan-yf-yf:fix/inline-lazy-type-alias

Conversation

@yangfan-yf-yf

@yangfan-yf-yf yangfan-yf-yf commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Inlining a variable used by a PEP 695 type alias can change when its initializer runs and which binding the alias resolves. For example, inlining target below changes the output from True to False:

original = int
target = original
type Alias = target
original = str
print(Alias.__value__ is int)

I added a preflight check that raises RefactoringError when a variable reference occurs within a type alias in the selected refactoring resources. This preserves the source instead of moving the initializer into a lazy context. A partial inline of an eager reference remains available with only_current=True, remove=False; removing a binding still used by a type alias in those resources is refused.

The regression tests execute source before and after the operation, covering side effects and late name binding as well as cross-module references, type parameter bounds, multiline and Unicode aliases, partial inlining, and unrelated/shadowed-name controls. On the original unmodified 2bd17a8 base (tested on 2026-09-29), 13 of the 17 new cases fail; all 17 pass with this change.

This follows the runtime-semantics testing direction in Discussion #875. It addresses ordinary variables referenced inside type aliases, separately from #874's handling of selecting a type-alias definition for inlining. Deferred function/variable annotations and type parameters outside type aliases are outside this change.

Validation after updating against master 22f5e50:

  • Windows, Python 3.12.3: full suite, 2166 passed, 12 skipped, 5 xfailed.
  • Windows, Python 3.14.7: new regression suite plus existing inline suite, 119 passed.
  • All five configured pre-commit checks passed for the changed files.
  • The type-alias guard remains unchanged; code samples follow the current textwrap.dedent() test guideline.

Checklist:

  • Added regression tests.
  • Updated CHANGELOG.md.

@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.36%. Comparing base (22f5e50) to head (5232586).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #890      +/-   ##
==========================================
+ Coverage   95.34%   95.36%   +0.02%     
==========================================
  Files         134      135       +1     
  Lines       26762    26885     +123     
==========================================
+ Hits        25516    25639     +123     
  Misses       1246     1246              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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