Fix --import-mode=importlib when root contains `__init__.py` file (#11420)

We cannot have an empty module name when importing a `__init__.py` file that
is at the rootdir.

Fixes #11417
This commit is contained in:
Bruno Oliveira 2023-09-10 09:57:40 -03:00 committed by GitHub
parent 71f265f1f3
commit e2acc1a99b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -623,8 +623,9 @@ def module_name_from_path(path: Path, root: Path) -> str:
# Use the parts for the relative path to the root path. # Use the parts for the relative path to the root path.
path_parts = relative_path.parts path_parts = relative_path.parts
# Module name for packages do not contain the __init__ file. # Module name for packages do not contain the __init__ file, unless
if path_parts[-1] == "__init__": # the `__init__.py` file is at the root.
if len(path_parts) >= 2 and path_parts[-1] == "__init__":
path_parts = path_parts[:-1] path_parts = path_parts[:-1]
return ".".join(path_parts) return ".".join(path_parts)

View File

@ -28,6 +28,7 @@ from _pytest.pathlib import resolve_package_path
from _pytest.pathlib import safe_exists from _pytest.pathlib import safe_exists
from _pytest.pathlib import symlink_or_skip from _pytest.pathlib import symlink_or_skip
from _pytest.pathlib import visit from _pytest.pathlib import visit
from _pytest.pytester import Pytester
from _pytest.tmpdir import TempPathFactory from _pytest.tmpdir import TempPathFactory
@ -592,6 +593,10 @@ class TestImportLibMode:
result = module_name_from_path(tmp_path / "src/app/__init__.py", tmp_path) result = module_name_from_path(tmp_path / "src/app/__init__.py", tmp_path)
assert result == "src.app" assert result == "src.app"
# Unless __init__.py file is at the root, in which case we cannot have an empty module name.
result = module_name_from_path(tmp_path / "__init__.py", tmp_path)
assert result == "__init__"
def test_insert_missing_modules( def test_insert_missing_modules(
self, monkeypatch: MonkeyPatch, tmp_path: Path self, monkeypatch: MonkeyPatch, tmp_path: Path
) -> None: ) -> None:
@ -663,6 +668,22 @@ class TestImportLibMode:
mod = import_path(init, root=tmp_path, mode=ImportMode.importlib) mod = import_path(init, root=tmp_path, mode=ImportMode.importlib)
assert len(mod.instance.INSTANCES) == 1 assert len(mod.instance.INSTANCES) == 1
def test_importlib_root_is_package(self, pytester: Pytester) -> None:
"""
Regression for importing a `__init__`.py file that is at the root
(#11417).
"""
pytester.makepyfile(__init__="")
pytester.makepyfile(
"""
def test_my_test():
assert True
"""
)
result = pytester.runpytest("--import-mode=importlib")
result.stdout.fnmatch_lines("* 1 passed *")
def test_safe_exists(tmp_path: Path) -> None: def test_safe_exists(tmp_path: Path) -> None:
d = tmp_path.joinpath("some_dir") d = tmp_path.joinpath("some_dir")