Skip to content

Commit

Permalink
Correctly import submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Apr 11, 2024
1 parent c3488ce commit 6c0aa36
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,7 @@ def import_path(
with contextlib.suppress(KeyError):
return sys.modules[module_name]

mod = _import_module_using_spec(
module_name, path, pkg_root, insert_modules=False
)
mod = _import_module_using_spec(module_name, path, insert_modules=False)
if mod is not None:
return mod

Expand All @@ -555,9 +553,7 @@ def import_path(
with contextlib.suppress(KeyError):
return sys.modules[module_name]

mod = _import_module_using_spec(
module_name, path, path.parent, insert_modules=True
)
mod = _import_module_using_spec(module_name, path, insert_modules=True)
if mod is None:
raise ImportError(f"Can't find module {module_name} at location {path}")
return mod
Expand Down Expand Up @@ -610,7 +606,7 @@ def import_path(


def _import_module_using_spec(
module_name: str, module_path: Path, module_location: Path, *, insert_modules: bool
module_name: str, module_path: Path, *, insert_modules: bool
) -> Optional[ModuleType]:
"""
Tries to import a module by its canonical name, path to the .py file, and its
Expand All @@ -627,7 +623,7 @@ def _import_module_using_spec(
# Checking with sys.meta_path first in case one of its hooks can import this module,
# such as our own assertion-rewrite hook.
for meta_importer in sys.meta_path:
spec = meta_importer.find_spec(module_name, [str(module_location)])
spec = meta_importer.find_spec(module_name, [str(module_path.parent)])
if spec is not None:
break
else:
Expand Down

0 comments on commit 6c0aa36

Please sign in to comment.