MCPcopy Create free account
hub / github.com/pytest-dev/pytest / insert_missing_modules

Function insert_missing_modules

src/_pytest/pathlib.py:593–610  ·  view source on GitHub ↗

Used by ``import_path`` to create intermediate modules when using mode=importlib. When we want to import a module as "src.tests.test_foo" for example, we need to create empty modules "src" and "src.tests" after inserting "src.tests.test_foo", otherwise "src.tests.test_foo" is not i

(modules: Dict[str, ModuleType], module_name: str)

Source from the content-addressed store, hash-verified

591
592
593def insert_missing_modules(modules: Dict[str, ModuleType], module_name: str) -> None:
594 """
595 Used by ``import_path`` to create intermediate modules when using mode=importlib.
596
597 When we want to import a module as "src.tests.test_foo" for example, we need
598 to create empty modules "src" and "src.tests" after inserting "src.tests.test_foo",
599 otherwise "src.tests.test_foo" is not importable by ``__import__``.
600 """
601 module_parts = module_name.split(".")
602 while module_name:
603 if module_name not in modules:
604 module = ModuleType(
605 module_name,
606 doc="Empty module created by pytest's importmode=importlib.",
607 )
608 modules[module_name] = module
609 module_parts.pop(-1)
610 module_name = ".".join(module_parts)
611
612
613def resolve_package_path(path: Path) -> Optional[Path]:

Callers 2

import_pathFunction · 0.85

Calls 1

popMethod · 0.80

Tested by 1