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

Đang mở Phù hợp với người mới
#926 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 7 ngày

@aliassar đang làm issue này rồi.

Từ ngày 3/11/2026.

  • #929 của @aliassar — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
developer-experience

Hướng nghiên cứu

Bắt đầu từ _get_single_expression_body() trong rope/refactor/extract.py, nơi extracted.lstrip()[0] gây ra IndexError khi chuỗi rỗng. Tái hiện bằng script trong issue, script này bao gồm vùng rỗng cho ExtractVariable và ExtractMethod, và vùng chỉ có khoảng trắng cho ExtractMethod. Hoàn thành khi cả ba trường hợp đều ném RefactoringError với thông báo rõ ràng, được kiểm tra trong _ExceptionalConditionChecker.base_conditions() hoặc sau _fix_start() và _fix_end() trong constructor.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Python
Star
2.2k
Fork
197
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-rope/rope

Tất cả issue của python-rope/rope

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.