Skip to content

Refuse variable inlining across lazy type alias boundaries - #890

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

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

Conversation

@yangfan-yf-yf

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. 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 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 unmodified base, 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:

  • Windows, Python 3.12.3: new regression suite, 17 passed.
  • Windows, Python 3.14.7: new regression suite plus existing inline suite, 119 passed.
  • Windows, Python 3.12.3: full suite, 2165 passed, 12 skipped, 5 xfailed.
  • All five configured pre-commit checks passed for the changed files.

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 (2bd17a8) to head (a5ca809).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #890      +/-   ##
==========================================
+ Coverage   95.34%   95.36%   +0.02%     
==========================================
  Files         134      135       +1     
  Lines       26819    26942     +123     
==========================================
+ Hits        25570    25693     +123     
  Misses       1249     1249              

☔ 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