Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog/13053.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a regression in pytest 8.3.4 where, when using ``--import-mode=importlib``, a directory containing py file with the same name would cause an ``ImportError``
13 changes: 10 additions & 3 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,16 @@ 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), str(module_path)]
)
module_name_of_meta = getattr(meta_importer.__class__, "__module__", "")
if module_name_of_meta == "_pytest.assertion.rewrite" and module_path.is_file():
# Import modules in subdirectories by module_path
# to ensure assertion rewrites are not missed (#12659).
find_spec_path = [str(module_location), str(module_path)]
else:
find_spec_path = [str(module_location)]

spec = meta_importer.find_spec(module_name, find_spec_path)

if spec_matches_module_path(spec, module_path):
break
else:
Expand Down
28 changes: 28 additions & 0 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,34 @@ def test():
]
)

def test_ns_multiple_levels_import_error(
self,
tmp_path: Path,
pytester: Pytester,
) -> None:
# Trigger condition 1: ns and file with the same name
file = pytester.path / "cow/moo/moo.py"
file.parent.mkdir(parents=True)
file.write_text("data=123", encoding="utf-8")

# Trigger condition 2: tests are located in ns
tests = pytester.path / "cow/moo/test_moo.py"

tests.write_text(
dedent(
"""
from cow.moo.moo import data

def test_moo():
print(data)
"""
),
encoding="utf-8",
)

result = pytester.runpytest("--import-mode=importlib")
assert result.ret == ExitCode.OK

@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
def test_incorrect_namespace_package(
self,
Expand Down