From 0e0b0daa73d6f1bc61ee9a84c678c8f1ae17964c Mon Sep 17 00:00:00 2001 From: user Date: Wed, 25 Jun 2025 17:40:46 -0400 Subject: [PATCH] Find submodule within pyz Closes #2770 (cherry picked from commit e44e17ab9d37cfdbe41f8af427dd6ebc7e5c4a1c) --- astroid/interpreter/_import/spec.py | 5 +++++ tests/test_manager.py | 11 +++++++++++ tests/testdata/python3/data/x.zip | Bin 0 -> 487 bytes 3 files changed, 16 insertions(+) create mode 100644 tests/testdata/python3/data/x.zip diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index 91782d8f5c..654d588876 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -318,6 +318,11 @@ def find_module( submodule_search_locations=path, ) + def contribute_to_path( + self, spec: ModuleSpec, processed: list[str] + ) -> Sequence[str] | None: + return spec.submodule_search_locations + class PathSpecFinder(Finder): """Finder based on importlib.machinery.PathFinder.""" diff --git a/tests/test_manager.py b/tests/test_manager.py index 9a7bbdb7ef..1b92ce4b44 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -277,6 +277,17 @@ def test_ast_from_module_name_pyz(self) -> None: finally: os.remove(linked_file_name) + def test_ast_from_module_name_pyz_with_submodule(self) -> None: + with self._restore_package_cache(): + archive_path = os.path.join(resources.RESOURCE_PATH, "x.zip") + sys.path.insert(0, archive_path) + module = self.manager.ast_from_module_name("xxx.test") + self.assertEqual(module.name, "xxx.test") + end = os.path.join(archive_path, "xxx", "test") + self.assertTrue( + module.file.endswith(end), f"{module.file} doesn't endswith {end}" + ) + def test_zip_import_data(self) -> None: """Check if zip_import_data works.""" with self._restore_package_cache(): diff --git a/tests/testdata/python3/data/x.zip b/tests/testdata/python3/data/x.zip new file mode 100644 index 0000000000000000000000000000000000000000..4f663fb34417fc24584d8899f6e411b984ef7bdc GIT binary patch literal 487 zcmWIWW@h1H00D`#>rr3^lwe_yVW_C6&<_paWMIy3jm#(l;?fFk21b?_%nS@*A^@)Y zankiD*;)UqMS;3Om>a6QB(=Ciub>iaNLGDhh9{;WDXD1+X{C9|8k$yI3P4bhSX|77 zFd5xl{7{qQ<1_OzOXA~Urh}Xa!Wd3uWRhdX(GF@7-19&KL7vBBAf_*2P6HV@18N|OpK-a8l?|wxff)!t0O^My4g&z+I9{0m literal 0 HcmV?d00001