Extract raises IndexError or ModuleSyntaxError instead of RefactoringError for an empty or whitespace-only region
メンテナーはふだん 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 3.13
- Rope master 46ebf37 (also 1.14.0)
- Context: rope-language-server issue #19
- 主要言語
- Python
- スター
- 2.2k
- フォーク
- 197
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 5
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python-rope/rope のほかの issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
python-rope/rope#927 ·
メンテナーはふだん 7 日以内に返信
-
simplify.real_code blanks t-strings, so renames inside them edit the wrong offset対応中かも @sean0407 が 9 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
python-rope/rope#913 ·
メンテナーはふだん 7 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
python-rope/rope#925 ·
メンテナーはふだん 7 日以内に返信
-
Extract method/variable and introduce parameter don't check whether the new name is already in useオープン
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
python-rope/rope#924 ·
メンテナーはふだん 7 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
python-rope/rope#923 ·
メンテナーはふだん 7 日以内に返信
python-rope/rope の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
earthtojake/text-to-cad#758 ·
メンテナーはふだん 1 日以内に返信
-
domain:upgrades enhancement needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
gke-labs/kube-agents#4634 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
agardnerIT/opamp-server-py#116 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
elviskahoro/sdk-python-attio#144 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信