Skip to content

Fix extraction of nested named expressions - #876

Open
yangfan-yf-yf wants to merge 3 commits into
python-rope:masterfrom
yangfan-yf-yf:fix-extract-nested-named-expression
Open

yangfan-yf-yf wants to merge 3 commits into
python-rope:masterfrom
yangfan-yf-yf:fix-extract-nested-named-expression

Conversation

@yangfan-yf-yf

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

Copy link
Copy Markdown
Contributor

Description

Extracting an expression with nested named expressions can produce invalid code such as if i, c := new_func(a):. The named-expression visitor counted the outer assignment without visiting its value, so it missed the inner assignment.

I updated the visitor to inspect the value and reject this unsafe extraction with RefactoringError. The regression verifies the refusal for the reported nested expression. The existing single-assignment extraction behavior remains covered by the suite.

Fixes #871

Checklist

  • I have added tests that prove my fix is effective
  • I have updated CHANGELOG.md

Validation

Synced with current upstream master 22f5e501, including the updated patchedast implementation and contribution guidelines.

  • Windows, Python 3.12.3: extraction, usefunction, and patchedast tests — 345 passed, 11 skipped, 1 xfailed.
  • Full suite — 2,149 passed, 12 skipped, 5 xfailed.
  • All five pre-commit checks and diff checks pass.
  • An actual execution probe confirms that the unsafe extraction raises RefactoringError, leaves the source unchanged, and preserves the original program's output.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #876   +/-   ##
=======================================
  Coverage   95.34%   95.34%           
=======================================
  Files         134      134           
  Lines       26762    26762           
=======================================
  Hits        25516    25516           
  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.

Extracting a function from walrus assignment with an internal walrus assignment produces invalid code

1 participant