Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion rope/contrib/autoimport/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ def generate_cache(
project are cached.
"""
if resources is None:
resources = self.project.get_python_files()
resources = [
f for f in self.project.get_python_files()
if "site-packages" not in f.pathlib.parts
]
job_set = task_handle.create_jobset(
"Generating autoimport cache", len(resources)
)
Expand Down
8 changes: 8 additions & 0 deletions rope/contrib/autoimport/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def get_modname_from_path(
package_name: str = package_path.stem
rel_path_parts = modpath.relative_to(package_path).parts
modname = ""
try:
site_packages_index = rel_path_parts.index("site-packages")
raise RuntimeError("No site-packages allowed here.", modpath)
except ValueError:
pass
else:
# If path includes "site-packages", we're interested in part after this.
rel_path_parts = rel_path_parts[site_packages_index + 1:]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's temporary code from my first attempt, but we probably need to leave here a check for no "site-packages" in path. Just in case.

if len(rel_path_parts) > 0:
for part in rel_path_parts[:-1]:
modname += part
Expand Down
7 changes: 7 additions & 0 deletions ropetest/contrib/autoimport/utilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def test_get_modname_single_file(typing_path):
assert utils.get_modname_from_path(typing_path, typing_path) == "typing"


def test_get_modname_external(example_external_package_path):
assert utils.get_modname_from_path(
example_external_package_path,
example_external_package_path,
) == "external_fixturepkg"


def test_get_modname_folder(
example_external_package_path,
example_external_package_module_path,
Expand Down