LocalToField doesn't validate the offset until get_changes()

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

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

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

評価

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

調査の方向性

offset のチェックは rope/refactor/localtofield.py の get_changes() から呼び出される _is_a_method_local() にあります。rope/refactor の他のリファクタリングはこのチェックを __init__ で行っているので、それらと比較してください。issue の再現スニペットを実行し、エラーが遅れて発生することを確認してください。モジュールレベルの変数に対してコンストラクタが RefactoringError を送出し、既存の localtofield テストモジュールのテストがそれをカバーしていれば完了です。

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

説明

Most refactorings check their offset in the constructor and raise RefactoringError straight away: Rename, create_inline(), MethodObject, IntroduceParameter and UseFunction all do. LocalToField only stores its arguments, so the check happens in get_changes().

Editor integrations build the refactoring object to decide whether to offer it in a menu, and only compute the changes when the user picks it. With LocalToField that shortcut doesn't work: building the object always succeeds, so "Convert local variable to field" is offered everywhere. pylsp-rope offers it this way.

To Reproduce

import pathlib
import tempfile

from rope.base.project import Project
from rope.refactor.localtofield import LocalToField

root = pathlib.Path(tempfile.mkdtemp())
(root / "m.py").write_text("x = 1\n")
project = Project(str(root), ropefolder=None)
refactoring = LocalToField(project, project.get_resource("m.py"), 0)
print("constructor accepted a module-level variable")
refactoring.get_changes()

Output on master (46ebf37), Python 3.13:

constructor accepted a module-level variable
rope.base.exceptions.RefactoringError: Convert local variable to field should be performed on a local variable of a method.

Expected behaviour

The constructor raises the RefactoringError. Moving the _is_a_method_local() check from get_changes() into __init__() would do it, and the check is cheap. I can send a PR with a test if that's acceptable.

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 を短くまとめたダイジェスト。