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
@@ -1,5 +1,6 @@
# **Upcoming release**

- #823 Fix autoimport.sqlite deriving module names from non-package folders (@deepakganesh78)
- ...

# Release 1.14.0
Expand Down
14 changes: 7 additions & 7 deletions rope/contrib/autoimport/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from rope.contrib.autoimport.parse import get_names
from rope.contrib.autoimport.utils import (
get_files,
get_modname_from_path,
get_package_tuple,
sort_and_deduplicate,
sort_and_deduplicate_tuple,
Expand Down Expand Up @@ -654,12 +653,13 @@ def _resource_to_module(
assert self.project_package.path
underlined = underlined if underlined else self.underlined
resource_path: Path = resource.pathlib
# The project doesn't need its name added to the path,
# since the standard python file layout accounts for that
# so we set add_package_name to False
resource_modname: str = get_modname_from_path(
resource_path, self.project_package.path, add_package_name=False
)
# Resolve the module name by walking up the package hierarchy from the
# resource (stopping at the first directory without an ``__init__.py``),
# rather than deriving it from the resource's path relative to the
# project root. The latter incorrectly includes intermediate,
# non-package directories in the module name.
# See https://github.com/python-rope/rope/issues/823
resource_modname: str = libutils.modname(resource)
return ModuleFile(
resource_path,
resource_modname,
Expand Down
17 changes: 17 additions & 0 deletions ropetest/contrib/autoimport/autoimporttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ def test_connection(project: Project, project2: Project):
assert ai1.connection is not ai3.connection


def test_resource_to_module_skips_non_package_folders(
autoimport: AutoImport,
project: Project,
):
# Intermediate folders that are not packages (no ``__init__.py``) must not
# be included in the derived module name.
# Regression test for https://github.com/python-rope/rope/issues/823
src = project.root.create_folder("src")
pkg = src.create_folder("mypkg")
pkg.create_file("__init__.py")
documents = pkg.create_file("documents.py")

module = autoimport._resource_to_module(documents)

assert module.modname == "mypkg.documents"


@contextmanager
def assert_database_is_reset(conn):
conn.execute("ALTER TABLE names ADD COLUMN deprecated_column")
Expand Down
Loading