Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefer_global_from_import #696

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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