review: do not search sys.modules if parent_module_name is empty

This commit is contained in:
Bruno Oliveira 2024-04-20 08:03:41 -03:00 committed by Bruno Oliveira
parent 91146eacfa
commit f62efb9934
1 changed files with 22 additions and 20 deletions

View File

@ -635,8 +635,10 @@ def _import_module_using_spec(
# Attempt to import the parent module, seems is our responsibility:
# https://github.com/python/cpython/blob/73906d5c908c1e0b73c5436faeff7d93698fc074/Lib/importlib/_bootstrap.py#L1308-L1311
parent_module_name, _, name = module_name.rpartition(".")
parent_module: Optional[ModuleType] = sys.modules.get(parent_module_name)
if parent_module is None and parent_module_name:
parent_module: Optional[ModuleType] = None
if parent_module_name:
parent_module = sys.modules.get(parent_module_name)
if parent_module is None:
# Find the directory of this module's parent.
parent_dir = (
module_path.parent.parent