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

movetest: add test_moving_modules_lazy_import #732

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion rope/base/worder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Copy link
Author

@SomeoneSerge SomeoneSerge Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this didn't break any tests (the only 3 failing ones are autoimports-related, and they fail on master too), and has fixed the ones I introduced. I'm still not sure I understood the original code correctly though

)

def is_from_statement(self, offset):
Expand Down
4 changes: 4 additions & 0 deletions ropetest/codeanalyzetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 41 additions & 0 deletions ropetest/refactor/movetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,47 @@ 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
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_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():
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():
from pkg2.pkg3.pkg4 import mod4
from pkg2.pkg3.pkg4.mod4 import thing

mod4
thing""")
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("""\
Expand Down
Loading