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 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
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 @@ -55,7 +55,7 @@

resolved_symbol = self.resolved_symbol
if resolved_symbol is not None and resolved_symbol.node_type == NodeType.FILE:
return self.alias.source

Check failure on line 58 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Editable[Any] | None" has no attribute "source" [union-attr]
return None

@property
Expand All @@ -76,9 +76,9 @@
return []

if not self.is_module_import():
return [self.imported_symbol]

Check failure on line 79 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: List item 0 has incompatible type "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]"; expected "Exportable[Any]" [list-item]

return self.imported_symbol.symbols + self.imported_symbol.imports

Check failure on line 81 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "Symbol[Any, Any]" of "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]" has no attribute "symbols" [union-attr]

Check failure on line 81 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "ExternalModule" of "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]" has no attribute "symbols" [union-attr]

Check failure on line 81 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "Import[Any]" of "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]" has no attribute "symbols" [union-attr]

Check failure on line 81 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "Symbol[Any, Any]" of "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]" has no attribute "imports" [union-attr]

Check failure on line 81 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "ExternalModule" of "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]" has no attribute "imports" [union-attr]

Check failure on line 81 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "Import[Any]" of "Symbol[Any, Any] | ExternalModule | PyFile | Import[Any]" has no attribute "imports" [union-attr]

@noapidoc
@reader
Expand All @@ -104,8 +104,8 @@
base_path,
module_source.replace(".", "/") + "/" + symbol_name + ".py",
)
if file := self.ctx.get_file(filepath):

Check failure on line 107 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "get_file" of "CodebaseContext" has incompatible type "str"; expected "PathLike[Any]" [arg-type]
return ImportResolution(from_file=file, symbol=None, imports_file=True)

Check failure on line 108 in src/codegen/sdk/python/import_resolution.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "from_file" to "ImportResolution" has incompatible type "SourceFile[Any, Any, Any, Any, Any, Any]"; expected "PyFile | None" [arg-type]

filepath = filepath.replace(".py", "/__init__.py")
if file := self.ctx.get_file(filepath):
Expand Down Expand Up @@ -133,6 +133,11 @@
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,23 @@ 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]