Skip to content

Commit

Permalink
Add prefer_global_from_import
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoolas25 committed Jun 10, 2023
1 parent 2524751 commit 3b9500b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# **Upcoming release**

- #624 Implement `nonlocal` keyword (@lieryan)
- #696 Introduce the `prefer_global_from_imports` configuration (@nicoolas25)

# Release 1.8.0

Expand Down
6 changes: 6 additions & 0 deletions docs/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def set_prefs(prefs):
#
# prefs["prefer_module_from_imports"] = False

# If `True`, rope will insert new name imports as
# `from <module> import <global>` by default.
#
# prefs["prefer_global_from_imports"] = False


# If `True`, rope will transform a comma list of imports into
# multiple separate import statements when organizing
# imports.
Expand Down
9 changes: 8 additions & 1 deletion rope/base/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ class Prefs:
prefer_module_from_imports: bool = field(
default=False,
description=dedent("""
If `True`, rope will insert new module imports as `from <package> import <module>`by default.
If `True`, rope will insert new module imports as `from <package> import <module>` by default.
"""),
)

prefer_global_from_imports: bool = field(
default=False,
description=dedent("""
If `True`, rope will insert new global imports as `from <module> import <global>` by default.
"""),
)

Expand Down
6 changes: 5 additions & 1 deletion rope/refactor/importutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,23 @@ def add_import(project, pymodule, module_name, name=None):
# from mod import name
if name is not None:
from_import = FromImport(module_name, 0, [(name, None)])
if project.prefs.get("prefer_global_from_imports"):
selected_import = from_import
names.append(name)
candidates.append(from_import)

# from pkg import mod
if "." in module_name:
pkg, mod = module_name.rsplit(".", 1)
from_import = FromImport(pkg, 0, [(mod, None)])
if project.prefs.get("prefer_module_from_imports"):
if project.prefs.get("prefer_module_from_imports") and not selected_import:
selected_import = from_import
candidates.append(from_import)
if name:
names.append(mod + "." + name)
else:
names.append(mod)

# import mod
normal_import = NormalImport([(module_name, None)])
if name:
Expand Down
46 changes: 46 additions & 0 deletions ropetest/refactor/movetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,52 @@ def a_function():
self.mod3.read(),
)

def test_adding_imports_prefer_from_global(self):
self.project.prefs["prefer_global_from_imports"] = True
self.mod1.write(dedent("""\
class AClass(object):
pass
def a_function():
pass
"""))
self.mod3.write(dedent("""\
import mod1
a_var = mod1.AClass()
mod1.a_function()"""))
# Move to mod4 which is in a different package
self._move(self.mod1, self.mod1.read().index("AClass") + 1, self.mod4)
self.assertEqual(
dedent("""\
import mod1
from pkg.mod4 import AClass
a_var = AClass()
mod1.a_function()"""),
self.mod3.read(),
)

def test_adding_imports_noprefer_from_global(self):
self.project.prefs["prefer_global_from_imports"] = False
self.mod1.write(dedent("""\
class AClass(object):
pass
def a_function():
pass
"""))
self.mod3.write(dedent("""\
import mod1
a_var = mod1.AClass()
mod1.a_function()"""))
# Move to mod4 which is in a different package
self._move(self.mod1, self.mod1.read().index("AClass") + 1, self.mod4)
self.assertEqual(
dedent("""\
import mod1
import pkg.mod4
a_var = pkg.mod4.AClass()
mod1.a_function()"""),
self.mod3.read(),
)

def test_adding_imports_prefer_from_module(self):
self.project.prefs["prefer_module_from_imports"] = True
self.mod1.write(dedent("""\
Expand Down

0 comments on commit 3b9500b

Please sign in to comment.