From ebb9b32ace25406f49b7c630fadf3fb2be82fe11 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Wed, 30 Nov 2022 16:17:47 +1100 Subject: [PATCH 1/3] Improve handling of whitespace around module names in from-import statement --- rope/refactor/patchedast.py | 6 ++++-- ropetest/refactor/patchedasttest.py | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/rope/refactor/patchedast.py b/rope/refactor/patchedast.py index 7ffa8de87..4c8c8ea9a 100644 --- a/rope/refactor/patchedast.py +++ b/rope/refactor/patchedast.py @@ -521,8 +521,10 @@ 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) diff --git a/ropetest/refactor/patchedasttest.py b/ropetest/refactor/patchedasttest.py index ebd1fe763..4dda0f276 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"]) From 5fc01fa5b088bb5063e321a8b8ca1b174c869e46 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Wed, 30 Nov 2022 16:30:47 +1100 Subject: [PATCH 2/3] Improve handling of whitespace around module names in import statement --- rope/refactor/patchedast.py | 2 +- ropetest/refactor/patchedasttest.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/rope/refactor/patchedast.py b/rope/refactor/patchedast.py index 4c8c8ea9a..5158cf53e 100644 --- a/rope/refactor/patchedast.py +++ b/rope/refactor/patchedast.py @@ -529,7 +529,7 @@ def _ImportFrom(self, node): 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 4dda0f276..3090cdf07 100644 --- a/ropetest/refactor/patchedasttest.py +++ b/ropetest/refactor/patchedasttest.py @@ -851,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) From 1032babbf1618bc615ba83bfa8e48f49737dac61 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Wed, 30 Nov 2022 16:35:59 +1100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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