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

Fix #559 Whitespace around import statements #560

Merged
merged 3 commits into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,6 +1,7 @@
# **Upcoming release**

- #533 Refactoring to Remove usage of unicode type
- #559 Improve handling of whitespace in import and from-import statements

# Release 1.5.1

Expand Down
8 changes: 5 additions & 3 deletions rope/refactor/patchedast.py
Expand Up @@ -521,13 +521,15 @@ def _AsyncFor(self, node):
def _ImportFrom(self, node):
children = ["from"]
if node.level:
children.append("." * node.level)
children.extend([node.module or "", "import"])
children.extend("." * node.level)
if node.module:
children.extend(node.module.split("."))
children.append("import")
children.extend(self._child_nodes(node.names, ","))
self._handle(node, children)

def _alias(self, node):
children = [node.name]
children = node.name.split(".")
if node.asname:
children.extend(["as", node.asname])
self._handle(node, children)
Expand Down
35 changes: 33 additions & 2 deletions ropetest/refactor/patchedasttest.py
Expand Up @@ -723,7 +723,7 @@ def test_from_node(self):
checker = _ResultChecker(self, ast_frag)
checker.check_region("ImportFrom", 0, len(source) - 1)
checker.check_children(
"ImportFrom", ["from", " ", "..", "", "x", " ", "import", " ", "alias"]
"ImportFrom", ["from", " ", ".", "", ".", "", "x", " ", "import", " ", "alias"]
)
checker.check_children("alias", ["y", " ", "as", " ", "z"])

Expand All @@ -734,7 +734,29 @@ def test_from_node_relative_import(self):
checker = _ResultChecker(self, ast_frag)
checker.check_region("ImportFrom", 0, len(source) - 1)
checker.check_children(
"ImportFrom", ["from", " ", ".", "", "", " ", "import", " ", "alias"]
"ImportFrom", ["from", " ", ".", " ", "import", " ", "alias"]
)
checker.check_children("alias", ["y", " ", "as", " ", "z"])

@testutils.only_for("2.5")
def test_from_node_whitespace_around_dots_1(self):
source = "from . . . import y as z\n"
ast_frag = patchedast.get_patched_ast(source, True)
checker = _ResultChecker(self, ast_frag)
checker.check_region("ImportFrom", 0, len(source) - 1)
checker.check_children(
"ImportFrom", ["from", " ", ".", " ", ".", " ", ".", " ", "import", " ", "alias"]
)
checker.check_children("alias", ["y", " ", "as", " ", "z"])

@testutils.only_for("2.5")
def test_from_node_whitespace_around_dots_2(self):
source = "from . a . b import y as z\n"
ast_frag = patchedast.get_patched_ast(source, True)
checker = _ResultChecker(self, ast_frag)
checker.check_region("ImportFrom", 0, len(source) - 1)
checker.check_children(
"ImportFrom", ["from", " ", ".", " ", "a", " . ", "b", " ", "import", " ", "alias"]
)
checker.check_children("alias", ["y", " ", "as", " ", "z"])

Expand Down Expand Up @@ -829,6 +851,15 @@ def test_import_node(self):
"Import", ["import", " ", "alias", "", ",", " ", "alias"]
)

def test_import_node_whitespace_around_dots(self):
source = "import a . b, b as c\n"
ast_frag = patchedast.get_patched_ast(source, True)
checker = _ResultChecker(self, ast_frag)
checker.check_region("Import", 0, len(source) - 1)
checker.check_children(
"Import", ["import", " ", "alias", "", ",", " ", "alias"]
)

def test_lambda_node(self):
source = "lambda a, b=1, *z: None\n"
ast_frag = patchedast.get_patched_ast(source, True)
Expand Down