MoveModule creates new top-level imports
Los mantenedores suelen responder en 7 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 42/100
Línea de trabajo
The extra top-level import comes from rope/refactor/move.py, around the lines the reporter links (roughly 594-596 and 769-773). Start by running the reproducer in the issue, which moves lazy.py into prefix/ while utils.py imports it inside a function, and check where the import is added to the module header. Done means the function-local import is rewritten in place and no new top-level import appears, with the existing move tests still passing.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Describe the bug
When MoveModule encounters an import statement inside a function (thus, evaluated at some unknown point later and maybe never), and the import statement concerns the module being moved, the calculated changes include both a modification of the original import statement and an extraneous import statement at the top of the file. This changes the order of imports and, in sufficiently cursed code-bases, may even result in a circular import.
Basically, the issue is that MoveModule appears to consider imports to be side effect-free, which sometimes they aren't.
The extra import is being added here:
https://github.com/python-rope/rope/blob/25586a6c6d4eb339fdf09188c195d24b43970efb/rope/refactor/move.py#L594-L596
https://github.com/python-rope/rope/blob/25586a6c6d4eb339fdf09188c195d24b43970efb/rope/refactor/move.py#L769-L773
To Reproduce
Steps to reproduce the behavior:
- Code before refactoring:
# a.py
def use_b_later():
import b
# b.py
import a
- Describe the refactoring you want to do
Rename (move) b into prefix.b.
- Expected code after refactoring:
# a.py
def use_b_later():
import prefix.b
# b.py
import a
- Describe the error or unexpected result that you are getting
# a.py
import prefix.b
def use_b_later():
import prefix.b
# b.py
import a
Reproducible example
$ python << EOF
import shutil
import tempfile
from contextlib import contextmanager
from os import walk
from pathlib import Path
from rope.base.project import Project
from rope.refactor.move import MoveModule
@contextmanager
def temp_tree():
path = tempfile.mkdtemp()
try:
yield Path(path)
finally:
shutil.rmtree(path, ignore_errors=True)
with temp_tree() as root:
(root / "utils.py").write_text(
"""
def use_lazy():
import lazy"""
)
(root / "lazy.py").write_text(
"""
import utils"""
)
(root / "prefix").mkdir()
(root / "prefix" / "__init__.py").touch()
project = Project(root)
new_package = project.get_folder("prefix")
lazy = project.get_module("lazy").get_resource()
print(MoveModule(project, lazy).get_changes(new_package).get_description())
EOF
Moving module <lazy>:
--- a/lazy.py
+++ b/lazy.py
@@ -1,2 +1 @@
-
-import utils+import utils
--- a/utils.py
+++ b/utils.py
@@ -1,3 +1,3 @@
-
+import prefix.lazy
def use_lazy():
- import lazy+ import prefix.lazy
rename from lazy.py
rename to prefix/lazy.py
Editor information (please complete the following information):
- Project Python version: 3.11
- Rope Python version: 3.11
- Rope version: 1.11.0 (also checked 1.9.0)
- Text editor/IDE and version: ...
Additional context
Context: trying to use Rope for some automation in https://github.com/SomeoneSerge/pkgs/blob/master/python-packages/by-name/pr/prefix-python-modules/prefix_python_modules.py
- Lenguaje dominante
- Python
- Estrellas
- 2.2k
- Forks
- 197
- Merge medio
- 2 d 13 h
- PR fusionados (30 d)
- 5
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python-rope/rope
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
python-rope/rope#927 ·
Los mantenedores suelen responder en 7 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
python-rope/rope#926 ·
Los mantenedores suelen responder en 7 días
-
simplify.real_code blanks t-strings, so renames inside them edit the wrong offsetPosiblemente ocupada @sean0407 la tomó hace 10 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
python-rope/rope#913 ·
Los mantenedores suelen responder en 7 días
-
Dificultad 4/5 3-5 días Aptitud para principiantes 30/100
python-rope/rope#925 ·
Los mantenedores suelen responder en 7 días
-
Extract method/variable and introduce parameter don't check whether the new name is already in useAbierto
Dificultad 3/5 1-2 días Aptitud para principiantes 62/100
python-rope/rope#924 ·
Los mantenedores suelen responder en 7 días
Todos los issues de python-rope/rope
Issues similares
-
ci-rerun: collect.py fails a whole pr-babysit cycle on one transient GitHub 5xx and never retriesAbiertofollow-up
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
Built-in QUBO fallback applies bitwise xor to floating binary statesPosiblemente ocupada @iloveeyjafjalla la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
qboson/kaiwu-pytorch-plugin#1702 ·
Los mantenedores suelen responder en 3 días
-
bug v2
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
modelcontextprotocol/python-sdk#3811 ·
Los mantenedores suelen responder en 1 día
-
cost:cheap practicality:seen-in-the-wild severity:medium
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
fairagro/m4.2_advanced_middleware_api#620 ·
Los mantenedores suelen responder en 1 día