Skip to content

fix(extract): raise RefactoringError for empty or whitespace-only region - #929

Merged
lieryan merged 2 commits into
python-rope:masterfrom
aliassar:fix-extract-empty-region-926
Nov 8, 2026
Merged

lieryan merged 2 commits into
python-rope:masterfrom
aliassar:fix-extract-empty-region-926

Conversation

@aliassar

@aliassar aliassar commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Description

Extract method and extract variable raised IndexError for an empty region and ModuleSyntaxError for a whitespace-only one, which an editor integration can't tell apart from a real crash. The extracted text was never checked for being empty, so it ended up in _get_single_expression_body() (extracted.lstrip()[0]) or in the generated wrapper function with no body. _ExceptionalConditionChecker.base_conditions() now raises RefactoringError ("Extracted piece should contain at least one expression or statement.") when the extracted text is blank, like the other bad-region cases.

Added tests for extract method (empty and whitespace-only) and extract variable (empty) in extracttest.py; they fail on master and pass now. The repro from the issue now prints RefactoringError for all three cases.

Fixes #926

Checklist (delete if not relevant):

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

ExtractMethod and ExtractVariable raised IndexError for an empty region
and ModuleSyntaxError for a whitespace-only one. Check for this in
_ExceptionalConditionChecker.base_conditions() so both report a
RefactoringError like other bad regions.

Fixes python-rope#926

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aliassar
aliassar force-pushed the fix-extract-empty-region-926 branch from 0f59eb5 to 57fb251 Compare November 3, 2026 17:53
@lieryan
lieryan enabled auto-merge November 8, 2026 05:41
@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.43%. Comparing base (7cb2f5a) to head (c7284ba).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #929   +/-   ##
=======================================
  Coverage   95.43%   95.43%           
=======================================
  Files         134      134           
  Lines       26826    26843   +17     
=======================================
+ Hits        25601    25618   +17     
  Misses       1225     1225           

☔ 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.

@lieryan
lieryan merged commit 282daa8 into python-rope:master Nov 8, 2026
24 checks passed
@lieryan

lieryan commented Nov 8, 2026

Copy link
Copy Markdown
Member

LGTM. Thanks for the pull request.

@allcontributors please add @aliassar for code contribution

@allcontributors

Copy link
Copy Markdown
Contributor

@lieryan

I've put up a pull request to add @aliassar! 🎉

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.

Extract raises IndexError or ModuleSyntaxError instead of RefactoringError for an empty or whitespace-only region

2 participants