diff --git a/CHANGELOG.md b/CHANGELOG.md index 4524af68a..a977cb797 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/rope/refactor/patchedast.py b/rope/refactor/patchedast.py index 7ffa8de87..5158cf53e 100644 --- a/rope/refactor/patchedast.py +++ b/rope/refactor/patchedast.py @@ -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) diff --git a/ropetest/refactor/patchedasttest.py b/ropetest/refactor/patchedasttest.py index ebd1fe763..3090cdf07 100644 --- a/ropetest/refactor/patchedasttest.py +++ b/ropetest/refactor/patchedasttest.py @@ -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"]) @@ -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"]) @@ -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)