Use _import_module_using_spec to import parent modules
Seems this is the right thing to do, as we will then also consider the parent modules for rewriting.
This commit is contained in:
parent
f4289181cb
commit
3c6caf65eb
|
@ -641,8 +641,24 @@ def _import_module_using_spec(
|
||||||
parent_module_name, _, name = module_name.rpartition(".")
|
parent_module_name, _, name = module_name.rpartition(".")
|
||||||
parent_module: Optional[ModuleType] = sys.modules.get(parent_module_name)
|
parent_module: Optional[ModuleType] = sys.modules.get(parent_module_name)
|
||||||
if parent_module is None and parent_module_name:
|
if parent_module is None and parent_module_name:
|
||||||
with contextlib.suppress(ModuleNotFoundError, ImportWarning):
|
# Find the directory of this module's parent.
|
||||||
parent_module = importlib.import_module(parent_module_name)
|
parent_dir = (
|
||||||
|
module_path.parent.parent
|
||||||
|
if module_path.name == "__init__.py"
|
||||||
|
else module_path.parent
|
||||||
|
)
|
||||||
|
# Consider the parent module path as its __init__.py file, if it has one.
|
||||||
|
parent_module_path = (
|
||||||
|
parent_dir / "__init__.py"
|
||||||
|
if (parent_dir / "__init__.py").is_file()
|
||||||
|
else parent_dir
|
||||||
|
)
|
||||||
|
parent_module = _import_module_using_spec(
|
||||||
|
parent_module_name,
|
||||||
|
parent_module_path,
|
||||||
|
parent_dir,
|
||||||
|
insert_modules=insert_modules,
|
||||||
|
)
|
||||||
|
|
||||||
# Find spec and import this module.
|
# Find spec and import this module.
|
||||||
mod = importlib.util.module_from_spec(spec)
|
mod = importlib.util.module_from_spec(spec)
|
||||||
|
|
Loading…
Reference in New Issue