Extract method/variable and introduce parameter don't check whether the new name is already in use
I maintainer di solito rispondono entro 7 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 62/100
Direzione di ricerca
Start with the entry points in the reproduction: extract.ExtractMethod, extract.ExtractVariable, introduce_parameter.IntroduceParameter and MethodObject.get_changes. Use the name-clash checks that rename already performs (the #761 to #769 fixes) as the reference for the error to raise. Done means each of the three reproductions raises RefactoringError instead of producing changed or broken output, and the existing refactoring tests still pass.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Extract method, extract variable and introduce parameter accept a new name that is already in use in the target scope. The result either doesn't run, or runs and silently changes behaviour. Rename already refuses several kinds of clashing names (#761 to #766, #768 and #769 were fixed that way, and #779 asks for the general check), so the other refactorings that introduce a name should do the same.
To Reproduce
import pathlib
import tempfile
from rope.base.project import Project
from rope.refactor import extract, introduce_parameter
def run(source, make):
root = pathlib.Path(tempfile.mkdtemp())
(root / "m.py").write_text(source)
project = Project(str(root), ropefolder=None)
changes = make(project, project.get_resource("m.py"), source)
print(changes.changes[0].new_contents)
# 1. Extract method into the name of an existing function
run(
"def helper():\n return 42\n\ndef f(a):\n return a + 1\n\nprint(helper())\n",
lambda p, r, s: extract.ExtractMethod(
p, r, s.index("a + 1"), s.index("a + 1") + 5
).get_changes("helper"),
)
# 2. Extract variable into the name of an existing parameter
run(
"def f(a, b):\n c = a * 2\n return c + b\n",
lambda p, r, s: extract.ExtractVariable(
p, r, s.index("a * 2"), s.index("a * 2") + 5
).get_changes("b"),
)
# 3. Introduce parameter with the name of an existing parameter
run(
"def f(a):\n return a + LIMIT\n\nLIMIT = 3\n",
lambda p, r, s: introduce_parameter.IntroduceParameter(
p, r, s.index("LIMIT")
).get_changes("a"),
)
Output on master (46ebf37), Python 3.13:
# 1. The second definition replaces the first one; print(helper()) now raises TypeError
def helper():
return 42
def f(a):
return helper(a)
def helper(a):
return a + 1
print(helper())
# 2. The parameter b is overwritten, so f(1, 10) returns 4 instead of 12
def f(a, b):
b = a * 2
c = b
return c + b
# 3. SyntaxError: duplicate argument 'a'
def f(a, a=LIMIT):
return a + a
LIMIT = 3
MethodObject.get_changes(classname) behaves the same way: with an existing class NewMethodObject, it adds a second class of that name.
Expected behaviour
Raise RefactoringError when the new name is already defined in the scope where the refactoring introduces it, or in a scope where the new definition would shadow it, as rename does for the cases fixed in #761 to #766, #768 and #769.
Editor information
- Python 3.13
- Rope master 46ebf37 (also 1.14.0)
- Found while adding Rope refactorings to rope-language-server. A language server can't prompt for a name before running the refactoring, so it has to choose one, and it can't rely on Rope to reject a clash.
- Lingua principale
- Python
- Stelle
- 2.2k
- Fork
- 197
- Merge medio
- 2g 13h
- PR unite (30g)
- 5
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-rope/rope
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
python-rope/rope#927 ·
I maintainer di solito rispondono entro 7 giorni
-
Extract raises IndexError or ModuleSyntaxError instead of RefactoringError for an empty or whitespace-only regionForse già presa @aliassar l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-rope/rope#926 ·
I maintainer di solito rispondono entro 7 giorni
-
simplify.real_code blanks t-strings, so renames inside them edit the wrong offsetForse già presa @sean0407 l’ha presa 10 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
python-rope/rope#913 ·
I maintainer di solito rispondono entro 7 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
python-rope/rope#925 ·
I maintainer di solito rispondono entro 7 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
python-rope/rope#923 ·
I maintainer di solito rispondono entro 7 giorni
Tutte le issue di python-rope/rope
Issue simili
-
Paraxial.trace returns NaN for an object at infinity when the entrance pupil is at the first surfaceAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement ios launcher
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
Substrait consumer doesn't recognise `shift_left` / `shift_right` from functions_arithmetic.yamlApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
apache/datafusion#27059 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
loteran/Arctis-Sound-Manager#368 ·
I maintainer di solito rispondono entro 1 giorno
-
feed submission
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
MobilityData/mobility-database-catalogs#1787 ·
I maintainer di solito rispondono entro 2 giorni