From 4a8aca1f68b192cf7273864c5c96ff89896e0bc3 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Wed, 8 Nov 2023 14:47:05 +0000 Subject: [PATCH 1/5] movetest: add test_moving_modules_lazy_import A test case for https://github.com/python-rope/rope/issues/731 --- ropetest/refactor/movetest.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ropetest/refactor/movetest.py b/ropetest/refactor/movetest.py index 508069aa0..7aae371db 100644 --- a/ropetest/refactor/movetest.py +++ b/ropetest/refactor/movetest.py @@ -598,6 +598,21 @@ def test_moving_modules_normal_import(self): print(pkg2.pkg3.pkg4.mod4)""") self.assertEqual(expected, self.mod1.read()) + def test_moving_modules_lazy_import(self): + pkg2 = testutils.create_package(self.project, "pkg2") + pkg3 = testutils.create_package(self.project, "pkg3", pkg2) + pkg4 = testutils.create_package(self.project, "pkg4", pkg3) + code = dedent("""\ + def import_later(): + import pkg.mod4""") + self.mod1.write(code) + self._move(self.mod4, None, pkg4) + self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None) + expected = dedent("""\ + def import_later(): + import pkg2.pkg3.pkg4.mod4""") + self.assertEqual(expected, self.mod1.read()) + def test_moving_package_with_from_and_normal_imports(self): pkg2 = testutils.create_package(self.project, "pkg2") code = dedent("""\ From 85c434be6d07381642f6ac22beee4fbdad5f8184 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Thu, 9 Nov 2023 00:27:54 +0000 Subject: [PATCH 2/5] is_import_statement: add a test for imports nested inside a function --- ropetest/codeanalyzetest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ropetest/codeanalyzetest.py b/ropetest/codeanalyzetest.py index 88a4caf96..ae9497eb5 100644 --- a/ropetest/codeanalyzetest.py +++ b/ropetest/codeanalyzetest.py @@ -292,6 +292,10 @@ def test_is_import_statement(self): result = a.b.c.d.f() + def import_later(): + + import lazy + +++++ """)) word_finder = worder.Worder(code) self.assert_equal_annotation( From e42c7b6096c2b799dbedef00b09235189c3eb9e6 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Thu, 9 Nov 2023 00:29:43 +0000 Subject: [PATCH 3/5] test_is_import_statement: fix lazy imports --- rope/base/worder.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/rope/base/worder.py b/rope/base/worder.py index 94089b5a8..64b6313d9 100644 --- a/rope/base/worder.py +++ b/rope/base/worder.py @@ -136,6 +136,18 @@ def __init__(self, code, raw): self.code = code self.raw = raw + def _find_next_word_start(self, offset): + current_offset = offset + + try: + end = self.code.index("\n", offset + 1) + except ValueError: + end = len(self.code) + + while current_offset < end and not self._is_id_char(current_offset): + current_offset += 1 + return current_offset + def _find_word_start(self, offset): current_offset = offset while current_offset >= 0 and self._is_id_char(current_offset): @@ -341,7 +353,7 @@ def is_import_statement(self, offset): line_start = self._get_line_start(last_import) return ( self._find_import_end(last_import + 7) >= offset - and self._find_word_start(line_start) == last_import + and self._find_next_word_start(line_start) == last_import ) def is_from_statement(self, offset): From 36b19ed9348951dc7a81f09114314877c860dcb6 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Thu, 9 Nov 2023 00:50:29 +0000 Subject: [PATCH 4/5] test_moving_modules_lazy_imports*: a test variant that still fails --- ropetest/refactor/movetest.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ropetest/refactor/movetest.py b/ropetest/refactor/movetest.py index 7aae371db..c5aff5922 100644 --- a/ropetest/refactor/movetest.py +++ b/ropetest/refactor/movetest.py @@ -613,6 +613,26 @@ def import_later(): import pkg2.pkg3.pkg4.mod4""") self.assertEqual(expected, self.mod1.read()) + # The previous test has only one occurrence of "lazy", which is inside an + # import statement. This test has an extra reference + def test_moving_modules_lazy_import_used(self): + pkg2 = testutils.create_package(self.project, "pkg2") + pkg3 = testutils.create_package(self.project, "pkg3", pkg2) + pkg4 = testutils.create_package(self.project, "pkg4", pkg3) + code = dedent("""\ + def import_later(): + import pkg.mod4 + pkg.mod4""") + self.mod1.write(code) + self._move(self.mod4, None, pkg4) + self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None) + expected = dedent("""\ + def import_later(): + import pkg2.pkg3.pkg4.mod4 + pkg2.pkg3.pkg4.mod4""") + self.assertEqual(expected, self.mod1.read()) + + def test_moving_package_with_from_and_normal_imports(self): pkg2 = testutils.create_package(self.project, "pkg2") code = dedent("""\ From d1c7c9a536aa6093b6d05d50a354455b0a59be93 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Sat, 18 Nov 2023 05:16:04 +0000 Subject: [PATCH 5/5] movetest: test lazy from-imports --- ropetest/refactor/movetest.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ropetest/refactor/movetest.py b/ropetest/refactor/movetest.py index c5aff5922..207e1f05c 100644 --- a/ropetest/refactor/movetest.py +++ b/ropetest/refactor/movetest.py @@ -604,32 +604,38 @@ def test_moving_modules_lazy_import(self): pkg4 = testutils.create_package(self.project, "pkg4", pkg3) code = dedent("""\ def import_later(): - import pkg.mod4""") + import pkg.mod4 + pkg.mod4""") self.mod1.write(code) self._move(self.mod4, None, pkg4) self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None) expected = dedent("""\ def import_later(): - import pkg2.pkg3.pkg4.mod4""") + import pkg2.pkg3.pkg4.mod4 + pkg2.pkg3.pkg4.mod4""") self.assertEqual(expected, self.mod1.read()) - # The previous test has only one occurrence of "lazy", which is inside an - # import statement. This test has an extra reference - def test_moving_modules_lazy_import_used(self): + def test_moving_modules_lazy_from_import(self): pkg2 = testutils.create_package(self.project, "pkg2") pkg3 = testutils.create_package(self.project, "pkg3", pkg2) pkg4 = testutils.create_package(self.project, "pkg4", pkg3) code = dedent("""\ def import_later(): - import pkg.mod4 - pkg.mod4""") + from pkg import mod4 + from pkg.mod4 import thing + + mod4 + thing""") self.mod1.write(code) self._move(self.mod4, None, pkg4) self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None) expected = dedent("""\ def import_later(): - import pkg2.pkg3.pkg4.mod4 - pkg2.pkg3.pkg4.mod4""") + from pkg2.pkg3.pkg4 import mod4 + from pkg2.pkg3.pkg4.mod4 import thing + + mod4 + thing""") self.assertEqual(expected, self.mod1.read())