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

オープン 初心者向け
#926 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 7 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

rope/refactor/extract.py の _get_single_expression_body() から始めてください。ここでは空文字列に対して extracted.lstrip()[0] が IndexError を発生させます。Issue のスクリプトで再現してください。このスクリプトは、ExtractVariable と ExtractMethod の空の範囲、および ExtractMethod の空白のみの範囲を扱います。3つのケースすべてで、わかりやすいメッセージとともに RefactoringError が発生すれば完了です。確認場所は _ExceptionalConditionChecker.base_conditions()、またはコンストラクタ内の _fix_start() と _fix_end() の後です。

索引モデルが issue の本文から書いたものです。

説明

ExtractMethod and ExtractVariable raise IndexError for an empty region, and ModuleSyntaxError about a generated wrapper for a region that contains only whitespace. Both are user mistakes, not internal errors, so they should be reported as RefactoringError, as the other bad-region cases are.

This matters for editor integrations. Neovim sends an empty range when the user requests code actions without a selection, so a language server calls extract with an empty region all the time. It has to tell "can't extract here" apart from a real crash it should log.

To Reproduce

import pathlib
import tempfile

from rope.base.project import Project
from rope.refactor import extract

source = "def f(a):\n    b = a + 1\n    return b\n"
root = pathlib.Path(tempfile.mkdtemp())
(root / "m.py").write_text(source)
project = Project(str(root), ropefolder=None)
resource = project.get_resource("m.py")

expr = source.index("a + 1")
indent = source.index("    b =")
for label, cls, start, end in [
    ("extract variable, empty region", extract.ExtractVariable, expr, expr),
    ("extract method, empty region", extract.ExtractMethod, expr, expr),
    ("extract method, whitespace only", extract.ExtractMethod, indent, indent + 4),
]:
    try:
        cls(project, resource, start, end).get_changes("new_name")
    except Exception as e:
        print(f"{label}: {type(e).__name__}: {e}")

Output on master (46ebf37), Python 3.13:

extract variable, empty region: IndexError: string index out of range
extract method, empty region: IndexError: string index out of range
extract method, whitespace only: ModuleSyntaxError: Syntax error in file <string> line <1>: expected an indented block after function definition on line 1

The IndexError comes from _get_single_expression_body(), which indexes extracted.lstrip()[0] on an empty string.

Expected behaviour

RefactoringError, with a message such as "Extracted piece should contain at least one expression or statement". Checking it in _ExceptionalConditionChecker.base_conditions(), or in the constructor after _fix_start() and _fix_end(), would cover both cases.

Editor information

主要言語
Python
スター
2.2k
フォーク
197
平均マージ
2日 13時間
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python-rope/rope のほかの issue

python-rope/rope の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。