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

[CG-10889] fix: Nested package #611

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 0 deletions src/codegen/sdk/python/import_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def resolve_import(self, base_path: str | None = None, *, add_module_name: str |
if base_path == "src":
# Try "test" next
return self.resolve_import(base_path="test", add_module_name=add_module_name)
if base_path == "test" and module_source:
# Try to resolve assuming package nested in repo
possible_package_base_path = module_source.split(".")[0]
if possible_package_base_path not in ("test", "src"):
return self.resolve_import(base_path=possible_package_base_path, add_module_name=add_module_name)

# if not G_override:
# for resolver in ctx.import_resolvers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,22 @@ def test_import_wildcard_preserves_import_resolution(tmpdir: str) -> None:
mainfile: SourceFile = codebase.get_file("file.py")

assert len(mainfile.ctx.edges) == 5

def test_import_nested_installable_resolution(tmpdir: str) -> None:
"""Tests that a nested installable resolves internally instead of as external"""
# language=python
content1 = """
TEST_CONST=5
"""
content2 = """from test_pack.test import TEST_CONST
test=TEST_CONST"""
with get_codebase_session(tmpdir=tmpdir, files={"test_pack/test_pack/test.py": content1, "test1.py": content2}) as codebase:
file1: SourceFile = codebase.get_file("test_pack/test_pack/test.py")
file2: SourceFile = codebase.get_file("test1.py")

symb = file1.get_symbol("TEST_CONST")
test = file2.get_symbol("test")
test_import = file2.get_import("TEST_CONST")

assert len(symb.usages) == 2
assert symb.symbol_usages == [test, test_import]
Loading