Skip to content

Commit e44e17a

Browse files
grayjkDanielNoord
authored andcommitted
Find submodule within pyz
Closes #2770
1 parent 855dd2c commit e44e17a

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

astroid/interpreter/_import/spec.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ def find_module(
325325
submodule_search_locations=path,
326326
)
327327

328+
def contribute_to_path(
329+
self, spec: ModuleSpec, processed: list[str]
330+
) -> Sequence[str] | None:
331+
return spec.submodule_search_locations
332+
328333

329334
class PathSpecFinder(Finder):
330335
"""Finder based on importlib.machinery.PathFinder."""

tests/test_manager.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ def test_ast_from_module_name_pyz(self) -> None:
284284
finally:
285285
os.remove(linked_file_name)
286286

287+
def test_ast_from_module_name_pyz_with_submodule(self) -> None:
288+
with self._restore_package_cache():
289+
archive_path = os.path.join(resources.RESOURCE_PATH, "x.zip")
290+
sys.path.insert(0, archive_path)
291+
module = self.manager.ast_from_module_name("xxx.test")
292+
self.assertEqual(module.name, "xxx.test")
293+
end = os.path.join(archive_path, "xxx", "test")
294+
self.assertTrue(
295+
module.file.endswith(end), f"{module.file} doesn't endswith {end}"
296+
)
297+
287298
def test_zip_import_data(self) -> None:
288299
"""Check if zip_import_data works."""
289300
with self._restore_package_cache():

tests/testdata/python3/data/x.zip

487 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)