Skip to content

Commit

Permalink
Merge pull request #560 from python-rope/lieryan-559-import-statement…
Browse files Browse the repository at this point in the history
…-whitespace

Fix #559 Whitespace around import statements
  • Loading branch information
lieryan committed Nov 30, 2022
2 parents 06d6a0d + 1032bab commit ecf503f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
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

0 comments on commit ecf503f

Please sign in to comment.