Extract raises IndexError or ModuleSyntaxError instead of RefactoringError for an empty or whitespace-only region
维护者通常 7 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
从 rope/refactor/extract.py 中的 _get_single_expression_body() 开始,其中 extracted.lstrip()[0] 在空字符串上会抛出 IndexError。使用 issue 中的脚本复现,该脚本覆盖 ExtractVariable 和 ExtractMethod 的空区域,以及 ExtractMethod 的纯空白区域。当三种情况都抛出带有清晰信息的 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 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 天内回复
相似的 Issue
-
Import errors are swallowed: PerthImplicitWatermarker silently becomes None可能已有人在做 @JonRC 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
resemble-ai/Perth#23 ·
-
correction metadata
难度 2/5 1 小时以内 新手友好度 65/100
acl-org/acl-anthology#10753 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
earthtojake/text-to-cad#758 ·
维护者通常 1 天内回复
-
难度 1/5 1-3 小时 新手友好度 70/100
DataTalksClub/faq#545 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复