MoveModule creates new top-level imports
Les mainteneurs répondent en général sous 7 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 42/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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
- Langage dominant
- Python
- Étoiles
- 2.2k
- Forks
- 197
- Merge moyen
- 2 j 13 h
- PR mergées (30 j)
- 5
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python-rope/rope
-
Difficulté 1/5 1-3 heures Accessibilité débutants 88/100
python-rope/rope#927 ·
Les mainteneurs répondent en général sous 7 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
python-rope/rope#926 ·
Les mainteneurs répondent en général sous 7 jours
-
simplify.real_code blanks t-strings, so renames inside them edit the wrong offsetPeut-être pris @sean0407 l’a pris il y a 10 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
python-rope/rope#913 ·
Les mainteneurs répondent en général sous 7 jours
-
Difficulté 4/5 3-5 jours Accessibilité débutants 30/100
python-rope/rope#925 ·
Les mainteneurs répondent en général sous 7 jours
-
Extract method/variable and introduce parameter don't check whether the new name is already in useOuverte
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
python-rope/rope#924 ·
Les mainteneurs répondent en général sous 7 jours
Toutes les issues de python-rope/rope
Issues similaires
-
cost:cheap practicality:seen-in-the-wild severity:medium
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
fairagro/m4.2_advanced_middleware_api#620 ·
Les mainteneurs répondent en général sous 1 jour
-
area:security effort:S priority:P2 provider:local-libvirt status:ready type:docs
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
randomparity/kdive#3495 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
microsoft/markitdown#2820 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 2 jours