Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misimport #12207

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,7 @@
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)

Check warning on line 553 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L553

Added line #L553 was not covered by tests
if mod is not None:
return mod

Expand All @@ -562,9 +560,7 @@
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)

Check warning on line 563 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L563

Added line #L563 was not covered by tests
if mod is None:
raise ImportError(f"Can't find module {module_name} at location {path}")
return mod
Expand Down Expand Up @@ -617,7 +613,7 @@


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 @@ -630,7 +626,7 @@
# 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)])

Check warning on line 629 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L629

Added line #L629 was not covered by tests
if spec_matches_module_path(spec, module_path):
break
else:
Expand Down Expand Up @@ -660,7 +656,6 @@
parent_module = _import_module_using_spec(
parent_module_name,
parent_module_path,
parent_dir,
insert_modules=insert_modules,
)

Expand Down
Loading