Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- #895 patchedast Starred and keyword now consumes their syntactically expected and ** (@lieryan)
- #896 patchedast cleanup and refactoring (@lieryan)
- #905 Ignore `node_modules` directories by default when scanning project files and source folders. (@marcoeangeli)
- #922 Classify autoimport project sources by path containment (@marlon-costa-dc)
- #926, #929 Raise RefactoringError instead of IndexError/ModuleSyntaxError when extracting an empty or whitespace-only region (@aliassar)
- #933 Add type annotations to project.py (@lieryan)
- #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl)
Expand Down
4 changes: 3 additions & 1 deletion rope/contrib/autoimport/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def get_package_source(
"""Detect the source of a given package. Rudimentary implementation."""
if name in sys.builtin_module_names:
return Source.BUILTIN
if project is not None and project.address in str(package):
if project is not None and package.resolve().is_relative_to(
project.root.pathlib.resolve()
):
return Source.PROJECT
if "site-packages" in package.parts:
return Source.SITE_PACKAGE
Expand Down
19 changes: 19 additions & 0 deletions ropetest/contrib/autoimport/utilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ def test_get_package_source_not_project(mod1_path):
assert utils.get_package_source(mod1_path, None, "") == Source.UNKNOWN


def test_get_package_source_project_prefix_is_not_ancestry(project):
sibling = Path(project.address + "-other") / "module.py"
assert utils.get_package_source(sibling, project, "module") == Source.UNKNOWN


def test_get_package_source_project_sibling(project):
sibling = project.root.pathlib.with_name(project.root.pathlib.name + "-other")
sibling.mkdir()
module = sibling / "outside.py"
module.touch()
try:
assert utils.get_package_source(module, project, "outside") == Source.UNKNOWN
escaped = project.root.pathlib / ".." / sibling.name / module.name
assert utils.get_package_source(escaped, project, "outside") == Source.UNKNOWN
finally:
module.unlink()
sibling.rmdir()


def test_get_package_source_pytest(example_external_package_path):
# pytest is not installed as part of the standard library
# but should be installed into site_packages,
Expand Down
Loading