Skip to content

Commit 4f15318

Browse files
committed
review: do not search sys.modules if parent_module_name is empty
1 parent 8b393d7 commit 4f15318

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/_pytest/pathlib.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -635,26 +635,28 @@ def _import_module_using_spec(
635635
# Attempt to import the parent module, seems is our responsibility:
636636
# https://github.com/python/cpython/blob/73906d5c908c1e0b73c5436faeff7d93698fc074/Lib/importlib/_bootstrap.py#L1308-L1311
637637
parent_module_name, _, name = module_name.rpartition(".")
638-
parent_module: Optional[ModuleType] = sys.modules.get(parent_module_name)
639-
if parent_module is None and parent_module_name:
640-
# Find the directory of this module's parent.
641-
parent_dir = (
642-
module_path.parent.parent
643-
if module_path.name == "__init__.py"
644-
else module_path.parent
645-
)
646-
# Consider the parent module path as its __init__.py file, if it has one.
647-
parent_module_path = (
648-
parent_dir / "__init__.py"
649-
if (parent_dir / "__init__.py").is_file()
650-
else parent_dir
651-
)
652-
parent_module = _import_module_using_spec(
653-
parent_module_name,
654-
parent_module_path,
655-
parent_dir,
656-
insert_modules=insert_modules,
657-
)
638+
parent_module: Optional[ModuleType] = None
639+
if parent_module_name:
640+
parent_module = sys.modules.get(parent_module_name)
641+
if parent_module is None:
642+
# Find the directory of this module's parent.
643+
parent_dir = (
644+
module_path.parent.parent
645+
if module_path.name == "__init__.py"
646+
else module_path.parent
647+
)
648+
# Consider the parent module path as its __init__.py file, if it has one.
649+
parent_module_path = (
650+
parent_dir / "__init__.py"
651+
if (parent_dir / "__init__.py").is_file()
652+
else parent_dir
653+
)
654+
parent_module = _import_module_using_spec(
655+
parent_module_name,
656+
parent_module_path,
657+
parent_dir,
658+
insert_modules=insert_modules,
659+
)
658660

659661
# Find spec and import this module.
660662
mod = importlib.util.module_from_spec(spec)

0 commit comments

Comments
 (0)