LocalToField doesn't validate the offset until get_changes()
Maintainers usually reply within 7 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
Research direction
The offset check lives in _is_a_method_local(), called from get_changes() in rope/refactor/localtofield.py; the other refactorings in rope/refactor do this check in __init__, so compare with them. Run the reproduction snippet from the issue to see the error arrive late. Done when the constructor raises RefactoringError for a module-level variable and a test in the existing localtofield test module covers it.
Written by the indexing model from the issue text.
Description
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 3.13
- Rope master 46ebf37 (also 1.14.0)
- Context: rope-language-server issue #19
- Dominant language
- Python
- Stars
- 2.2k
- Forks
- 197
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 5
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-rope/rope
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
python-rope/rope#926 ·
Maintainers usually reply within 7 days
-
simplify.real_code blanks t-strings, so renames inside them edit the wrong offsetPossibly taken @sean0407 claimed this 10 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
python-rope/rope#913 ·
Maintainers usually reply within 7 days
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
python-rope/rope#925 ·
Maintainers usually reply within 7 days
-
Extract method/variable and introduce parameter don't check whether the new name is already in useOpen
Difficulty 3/5 1-2 days Newbie friendliness 62/100
python-rope/rope#924 ·
Maintainers usually reply within 7 days
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-rope/rope#923 ·
Maintainers usually reply within 7 days
All issues in python-rope/rope
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
yt-project/yt#5624 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
NousResearch/hermes-agent#148376 ·
Maintainers usually reply within 1 day
-
Analysis
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SACGF/variantgrid#2615 ·
Maintainers usually reply within 1 day