From 49361e0f5aabb1194c543d7c08eab2f0cd4aef79 Mon Sep 17 00:00:00 2001 From: tomcodegen Date: Fri, 21 Feb 2025 15:08:00 -0800 Subject: [PATCH 1/2] fix --- src/codegen/sdk/python/import_resolution.py | 5 +++++ .../test_import_resolution.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/codegen/sdk/python/import_resolution.py b/src/codegen/sdk/python/import_resolution.py index a80bb2ada..62574c410 100644 --- a/src/codegen/sdk/python/import_resolution.py +++ b/src/codegen/sdk/python/import_resolution.py @@ -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: diff --git a/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py b/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py index 6fd9cbe7b..b8b69917a 100644 --- a/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py +++ b/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py @@ -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] From 218ceb6f652606d9ac72da1d93c3a801c76140f4 Mon Sep 17 00:00:00 2001 From: tomcodgen <191515280+tomcodgen@users.noreply.github.com> Date: Fri, 21 Feb 2025 23:09:49 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- .../sdk/python/import_resolution/test_import_resolution.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py b/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py index b8b69917a..f7cb87bd4 100644 --- a/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py +++ b/tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py @@ -369,6 +369,7 @@ def test_import_wildcard_preserves_import_resolution(tmpdir: str) -> None: 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