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

fix: Fix import paths with site-packages as part of package name #756

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 14 additions & 1 deletion rope/base/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,22 @@ def __init__(
super().__init__(fscommands)
self.ignored = _ResourceMatcher()
self.file_list = _FileListCacher(self)

extra_ignores = [
p.pathlib.relative_to(self.root.pathlib)
for p in self.get_python_path_folders()
if p.pathlib.is_relative_to(self.root.pathlib)
and "site-packages" in p.pathlib.parts
]
for p in extra_ignores:
# I'm using this approach, because self.prefs.add after _init_prefs
# does not work for some reason.
prefs["ignored_resources"].append(str(p))
Comment on lines +228 to +237
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 my second attempt, and i need feedback here.

Is it good approach, or maybe some other ideas?


self._init_prefs(prefs)
if ropefolder is not None:
self.prefs.add("ignored_resources", ropefolder)

self._init_source_folders()

def __repr__(self):
Expand Down Expand Up @@ -281,7 +294,7 @@ def _init_other_parts(self):
# Forcing the creation of `self.pycore` to register observers
self.pycore # pylint: disable=pointless-statement

def is_ignored(self, resource):
def is_ignored(self, resource: Resource):
return self.ignored.does_match(resource)

def sync(self):
Expand Down
2 changes: 1 addition & 1 deletion rope/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def _add_pattern(self, pattern):
re_pattern = "^(.*/)?" + re_pattern + "(/.*)?$"
self.compiled_patterns.append(re.compile(re_pattern))

def does_match(self, resource):
def does_match(self, resource: Resource):
for pattern in self.compiled_patterns:
if pattern.match(resource.path):
return True
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
Loading