diff --git a/AUTHORS b/AUTHORS index 879097622..4378f4c0d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -309,6 +309,7 @@ Pavel Karateev Paweł Adamczak Pedro Algarvio Petter Strandmark +Philipp A. Philipp Loose Pierre Sassoulas Pieter Mulder diff --git a/changelog/11931.bugfix.rst b/changelog/11931.bugfix.rst new file mode 100644 index 000000000..5d71eee82 --- /dev/null +++ b/changelog/11931.bugfix.rst @@ -0,0 +1 @@ +Fix some instances of importing doctests’ parent modules when using `--import-mode=importlib`. diff --git a/pyproject.toml b/pyproject.toml index 15a855ce6..560d63203 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -173,6 +173,9 @@ ignore = [ [tool.ruff.format] docstring-code-format = true +[tool.ruff.lint] +allowed-confusables = ["’", "×"] + [tool.ruff.lint.pycodestyle] # In order to be able to format for 88 char in ruff format max-line-length = 120 diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 351223f76..928a3d0db 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -617,7 +617,7 @@ def module_name_from_path(path: Path, root: Path) -> str: _module_name_from_path(path, dir) for dir in itertools.chain([root], map(Path, sys.path)) ) - return ".".join(min(candidates, key=len)) + return ".".join(min(candidates, key=len)) # type: ignore[arg-type] def _module_name_from_path(path: Path, root: Path) -> tuple[str, ...]: diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index f22add229..4f8b5b07b 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -691,6 +691,7 @@ class TestImportLibMode: init = pkgdir / "__init__.py" init.write_text("", encoding="ascii") + # the PyTest root is `proj_dir`, but the package is imported from `pkgs_dir` mod = import_path(init, root=proj_dir, mode=ImportMode.importlib) # assert that it’s imported with the canonical name, not “path.to.package.” mod_names = [n for n, m in sys.modules.items() if m is mod]