2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2023-09-07 23:49:25 +08:00
|
|
|
import errno
|
2024-04-10 00:21:51 +08:00
|
|
|
import importlib.abc
|
|
|
|
import importlib.machinery
|
2019-06-23 06:02:32 +08:00
|
|
|
import os.path
|
2020-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
2020-12-19 21:16:01 +08:00
|
|
|
import pickle
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
import shutil
|
2018-09-18 07:01:01 +08:00
|
|
|
import sys
|
2020-06-13 22:29:01 +08:00
|
|
|
from textwrap import dedent
|
2020-12-19 21:16:01 +08:00
|
|
|
from types import ModuleType
|
2021-04-06 04:10:03 +08:00
|
|
|
from typing import Any
|
2020-12-19 21:16:01 +08:00
|
|
|
from typing import Generator
|
2023-07-01 23:37:46 +08:00
|
|
|
from typing import Iterator
|
2024-04-10 00:21:51 +08:00
|
|
|
from typing import Optional
|
|
|
|
from typing import Sequence
|
2024-02-28 02:13:28 +08:00
|
|
|
from typing import Tuple
|
2020-06-12 03:47:59 +08:00
|
|
|
import unittest.mock
|
2018-09-18 07:01:01 +08:00
|
|
|
|
2020-12-12 19:49:58 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2020-08-04 15:12:27 +08:00
|
|
|
from _pytest.pathlib import bestrelpath
|
|
|
|
from _pytest.pathlib import commonpath
|
2024-04-10 00:21:51 +08:00
|
|
|
from _pytest.pathlib import compute_module_name
|
2024-02-28 02:13:28 +08:00
|
|
|
from _pytest.pathlib import CouldNotResolvePathError
|
2020-06-12 03:47:59 +08:00
|
|
|
from _pytest.pathlib import ensure_deletable
|
2018-10-01 19:44:52 +08:00
|
|
|
from _pytest.pathlib import fnmatch_ex
|
2020-06-02 19:56:33 +08:00
|
|
|
from _pytest.pathlib import get_extended_length_path_str
|
2018-10-31 00:31:55 +08:00
|
|
|
from _pytest.pathlib import get_lock_path
|
2020-06-13 22:29:01 +08:00
|
|
|
from _pytest.pathlib import import_path
|
2023-09-06 06:42:40 +08:00
|
|
|
from _pytest.pathlib import ImportMode
|
2020-06-13 22:29:01 +08:00
|
|
|
from _pytest.pathlib import ImportPathMismatchError
|
2021-04-06 04:10:03 +08:00
|
|
|
from _pytest.pathlib import insert_missing_modules
|
2024-04-10 00:21:51 +08:00
|
|
|
from _pytest.pathlib import is_importable
|
2018-10-31 00:31:55 +08:00
|
|
|
from _pytest.pathlib import maybe_delete_a_numbered_dir
|
2021-04-06 04:10:03 +08:00
|
|
|
from _pytest.pathlib import module_name_from_path
|
2020-06-13 22:29:01 +08:00
|
|
|
from _pytest.pathlib import resolve_package_path
|
2024-02-28 02:13:28 +08:00
|
|
|
from _pytest.pathlib import resolve_pkg_root_and_module_name
|
2023-09-07 23:49:25 +08:00
|
|
|
from _pytest.pathlib import safe_exists
|
2020-10-28 15:27:43 +08:00
|
|
|
from _pytest.pathlib import symlink_or_skip
|
|
|
|
from _pytest.pathlib import visit
|
2023-09-10 20:57:40 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2024-04-10 00:21:51 +08:00
|
|
|
from _pytest.pytester import RunResult
|
2020-12-19 21:16:01 +08:00
|
|
|
from _pytest.tmpdir import TempPathFactory
|
2018-09-18 07:01:01 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def autouse_pytester(pytester: Pytester) -> None:
|
|
|
|
"""
|
|
|
|
Fixture to make pytester() being autouse for all tests in this module.
|
|
|
|
|
|
|
|
pytester makes sure to restore sys.path to its previous state, and many tests in this module
|
|
|
|
import modules and change sys.path because of that, so common module names such as "test" or "test.conftest"
|
|
|
|
end up leaking to tests in other modules.
|
|
|
|
|
|
|
|
Note: we might consider extracting the sys.path restoration aspect into its own fixture, and apply it
|
|
|
|
to the entire test suite always.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-06-13 22:29:01 +08:00
|
|
|
class TestFNMatcherPort:
|
2021-03-21 03:52:36 +08:00
|
|
|
"""Test our port of py.common.FNMatcher (fnmatch_ex)."""
|
2018-09-18 07:01:01 +08:00
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
drv1 = "c:"
|
|
|
|
drv2 = "d:"
|
|
|
|
else:
|
|
|
|
drv1 = "/c"
|
|
|
|
drv2 = "/d"
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"pattern, path",
|
|
|
|
[
|
|
|
|
("*.py", "foo.py"),
|
|
|
|
("*.py", "bar/foo.py"),
|
|
|
|
("test_*.py", "foo/test_foo.py"),
|
|
|
|
("tests/*.py", "tests/foo.py"),
|
2021-03-21 03:52:36 +08:00
|
|
|
(f"{drv1}/*.py", f"{drv1}/foo.py"),
|
|
|
|
(f"{drv1}/foo/*.py", f"{drv1}/foo/foo.py"),
|
2018-09-18 07:01:01 +08:00
|
|
|
("tests/**/test*.py", "tests/foo/test_foo.py"),
|
|
|
|
("tests/**/doc/test*.py", "tests/foo/bar/doc/test_foo.py"),
|
|
|
|
("tests/**/doc/**/test*.py", "tests/foo/doc/bar/test_foo.py"),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 03:52:36 +08:00
|
|
|
def test_matching(self, pattern: str, path: str) -> None:
|
|
|
|
assert fnmatch_ex(pattern, path)
|
2018-09-18 07:01:01 +08:00
|
|
|
|
2021-03-21 03:52:36 +08:00
|
|
|
def test_matching_abspath(self) -> None:
|
2019-06-23 06:02:32 +08:00
|
|
|
abspath = os.path.abspath(os.path.join("tests/foo.py"))
|
2021-03-21 03:52:36 +08:00
|
|
|
assert fnmatch_ex("tests/foo.py", abspath)
|
2019-06-23 06:02:32 +08:00
|
|
|
|
2018-09-18 07:01:01 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"pattern, path",
|
|
|
|
[
|
|
|
|
("*.py", "foo.pyc"),
|
|
|
|
("*.py", "foo/foo.pyc"),
|
|
|
|
("tests/*.py", "foo/foo.py"),
|
2021-03-21 03:52:36 +08:00
|
|
|
(f"{drv1}/*.py", f"{drv2}/foo.py"),
|
|
|
|
(f"{drv1}/foo/*.py", f"{drv2}/foo/foo.py"),
|
2018-09-18 07:01:01 +08:00
|
|
|
("tests/**/test*.py", "tests/foo.py"),
|
|
|
|
("tests/**/test*.py", "foo/test_foo.py"),
|
|
|
|
("tests/**/doc/test*.py", "tests/foo/bar/doc/foo.py"),
|
|
|
|
("tests/**/doc/test*.py", "tests/foo/bar/test_foo.py"),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 03:52:36 +08:00
|
|
|
def test_not_matching(self, pattern: str, path: str) -> None:
|
|
|
|
assert not fnmatch_ex(pattern, path)
|
2018-10-31 00:31:55 +08:00
|
|
|
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
@pytest.fixture(params=[True, False])
|
|
|
|
def ns_param(request: pytest.FixtureRequest) -> bool:
|
|
|
|
"""
|
|
|
|
Simple parametrized fixture for tests which call import_path() with consider_namespace_packages
|
|
|
|
using True and False.
|
|
|
|
"""
|
|
|
|
return bool(request.param)
|
|
|
|
|
|
|
|
|
2020-06-13 22:29:01 +08:00
|
|
|
class TestImportPath:
|
|
|
|
"""
|
|
|
|
|
|
|
|
Most of the tests here were copied from py lib's tests for "py.local.path.pyimport".
|
|
|
|
|
|
|
|
Having our own pyimport-like function is inline with removing py.path dependency in the future.
|
|
|
|
"""
|
|
|
|
|
2020-06-25 19:05:46 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2020-12-19 21:16:01 +08:00
|
|
|
def path1(self, tmp_path_factory: TempPathFactory) -> Generator[Path, None, None]:
|
|
|
|
path = tmp_path_factory.mktemp("path")
|
2020-06-13 22:29:01 +08:00
|
|
|
self.setuptestfs(path)
|
|
|
|
yield path
|
2020-12-19 21:16:01 +08:00
|
|
|
assert path.joinpath("samplefile").exists()
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2022-10-20 10:07:53 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def preserve_sys(self):
|
|
|
|
with unittest.mock.patch.dict(sys.modules):
|
|
|
|
with unittest.mock.patch.object(sys, "path", list(sys.path)):
|
|
|
|
yield
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def setuptestfs(self, path: Path) -> None:
|
2020-06-13 22:29:01 +08:00
|
|
|
# print "setting up test fs for", repr(path)
|
2020-12-19 21:16:01 +08:00
|
|
|
samplefile = path / "samplefile"
|
2023-06-20 19:55:39 +08:00
|
|
|
samplefile.write_text("samplefile\n", encoding="utf-8")
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
execfile = path / "execfile"
|
2023-06-20 19:55:39 +08:00
|
|
|
execfile.write_text("x=42", encoding="utf-8")
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
execfilepy = path / "execfile.py"
|
2023-06-20 19:55:39 +08:00
|
|
|
execfilepy.write_text("x=42", encoding="utf-8")
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
d = {1: 2, "hello": "world", "answer": 42}
|
2020-12-19 21:16:01 +08:00
|
|
|
path.joinpath("samplepickle").write_bytes(pickle.dumps(d, 1))
|
|
|
|
|
|
|
|
sampledir = path / "sampledir"
|
|
|
|
sampledir.mkdir()
|
|
|
|
sampledir.joinpath("otherfile").touch()
|
|
|
|
|
|
|
|
otherdir = path / "otherdir"
|
|
|
|
otherdir.mkdir()
|
|
|
|
otherdir.joinpath("__init__.py").touch()
|
|
|
|
|
|
|
|
module_a = otherdir / "a.py"
|
2023-06-20 19:55:40 +08:00
|
|
|
module_a.write_text("from .b import stuff as result\n", encoding="utf-8")
|
2020-12-19 21:16:01 +08:00
|
|
|
module_b = otherdir / "b.py"
|
2023-06-20 19:55:40 +08:00
|
|
|
module_b.write_text('stuff="got it"\n', encoding="utf-8")
|
2020-12-19 21:16:01 +08:00
|
|
|
module_c = otherdir / "c.py"
|
|
|
|
module_c.write_text(
|
2020-06-13 22:29:01 +08:00
|
|
|
dedent(
|
|
|
|
"""
|
2021-10-16 07:50:05 +08:00
|
|
|
import pluggy;
|
2020-06-13 22:29:01 +08:00
|
|
|
import otherdir.a
|
|
|
|
value = otherdir.a.result
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2020-06-13 22:29:01 +08:00
|
|
|
)
|
2020-12-19 21:16:01 +08:00
|
|
|
module_d = otherdir / "d.py"
|
|
|
|
module_d.write_text(
|
2020-06-13 22:29:01 +08:00
|
|
|
dedent(
|
|
|
|
"""
|
2021-10-16 07:50:05 +08:00
|
|
|
import pluggy;
|
2020-06-13 22:29:01 +08:00
|
|
|
from otherdir import a
|
|
|
|
value2 = a.result
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2020-06-13 22:29:01 +08:00
|
|
|
)
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_smoke_test(self, path1: Path, ns_param: bool) -> None:
|
2024-03-02 22:46:54 +08:00
|
|
|
obj = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
path1 / "execfile.py", root=path1, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert obj.x == 42
|
2020-06-13 22:29:01 +08:00
|
|
|
assert obj.__name__ == "execfile"
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_import_path_missing_file(self, path1: Path, ns_param: bool) -> None:
|
2021-12-30 06:52:16 +08:00
|
|
|
with pytest.raises(ImportPathMismatchError):
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
path1 / "sampledir", root=path1, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2021-12-30 06:52:16 +08:00
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_renamed_dir_creates_mismatch(
|
2024-03-02 23:03:08 +08:00
|
|
|
self, tmp_path: Path, monkeypatch: MonkeyPatch, ns_param: bool
|
2020-12-19 21:16:01 +08:00
|
|
|
) -> None:
|
|
|
|
tmp_path.joinpath("a").mkdir()
|
|
|
|
p = tmp_path.joinpath("a", "test_x123.py")
|
|
|
|
p.touch()
|
2024-03-02 23:03:08 +08:00
|
|
|
import_path(p, root=tmp_path, consider_namespace_packages=ns_param)
|
2020-12-19 21:16:01 +08:00
|
|
|
tmp_path.joinpath("a").rename(tmp_path.joinpath("b"))
|
2020-06-13 22:29:01 +08:00
|
|
|
with pytest.raises(ImportPathMismatchError):
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
|
|
|
tmp_path.joinpath("b", "test_x123.py"),
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
# Errors can be ignored.
|
|
|
|
monkeypatch.setenv("PY_IGNORE_IMPORTMISMATCH", "1")
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
|
|
|
tmp_path.joinpath("b", "test_x123.py"),
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
# PY_IGNORE_IMPORTMISMATCH=0 does not ignore error.
|
|
|
|
monkeypatch.setenv("PY_IGNORE_IMPORTMISMATCH", "0")
|
|
|
|
with pytest.raises(ImportPathMismatchError):
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
|
|
|
tmp_path.joinpath("b", "test_x123.py"),
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_messy_name(self, tmp_path: Path, ns_param: bool) -> None:
|
2021-04-26 23:44:27 +08:00
|
|
|
# https://bitbucket.org/hpk42/py-trunk/issue/129
|
2020-12-19 21:16:01 +08:00
|
|
|
path = tmp_path / "foo__init__.py"
|
|
|
|
path.touch()
|
2024-03-02 23:03:08 +08:00
|
|
|
module = import_path(path, root=tmp_path, consider_namespace_packages=ns_param)
|
2020-06-13 22:29:01 +08:00
|
|
|
assert module.__name__ == "foo__init__"
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_dir(self, tmp_path: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
p = tmp_path / "hello_123"
|
|
|
|
p.mkdir()
|
|
|
|
p_init = p / "__init__.py"
|
|
|
|
p_init.touch()
|
2024-03-02 23:03:08 +08:00
|
|
|
m = import_path(p, root=tmp_path, consider_namespace_packages=ns_param)
|
2020-06-13 22:29:01 +08:00
|
|
|
assert m.__name__ == "hello_123"
|
2024-03-02 23:03:08 +08:00
|
|
|
m = import_path(p_init, root=tmp_path, consider_namespace_packages=ns_param)
|
2020-06-13 22:29:01 +08:00
|
|
|
assert m.__name__ == "hello_123"
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_a(self, path1: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
otherdir = path1 / "otherdir"
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
otherdir / "a.py", root=path1, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert mod.result == "got it"
|
2020-06-13 22:29:01 +08:00
|
|
|
assert mod.__name__ == "otherdir.a"
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_b(self, path1: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
otherdir = path1 / "otherdir"
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
otherdir / "b.py", root=path1, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert mod.stuff == "got it"
|
2020-06-13 22:29:01 +08:00
|
|
|
assert mod.__name__ == "otherdir.b"
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_c(self, path1: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
otherdir = path1 / "otherdir"
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
otherdir / "c.py", root=path1, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert mod.value == "got it"
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_d(self, path1: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
otherdir = path1 / "otherdir"
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
otherdir / "d.py", root=path1, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert mod.value2 == "got it"
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_import_after(self, tmp_path: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
tmp_path.joinpath("xxxpackage").mkdir()
|
|
|
|
tmp_path.joinpath("xxxpackage", "__init__.py").touch()
|
|
|
|
mod1path = tmp_path.joinpath("xxxpackage", "module1.py")
|
|
|
|
mod1path.touch()
|
2024-03-02 23:03:08 +08:00
|
|
|
mod1 = import_path(
|
|
|
|
mod1path, root=tmp_path, consider_namespace_packages=ns_param
|
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
assert mod1.__name__ == "xxxpackage.module1"
|
|
|
|
from xxxpackage import module1
|
|
|
|
|
|
|
|
assert module1 is mod1
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_check_filepath_consistency(
|
2024-03-02 23:03:08 +08:00
|
|
|
self, monkeypatch: MonkeyPatch, tmp_path: Path, ns_param: bool
|
2020-12-19 21:16:01 +08:00
|
|
|
) -> None:
|
2020-06-13 22:29:01 +08:00
|
|
|
name = "pointsback123"
|
2020-12-19 21:16:01 +08:00
|
|
|
p = tmp_path.joinpath(name + ".py")
|
|
|
|
p.touch()
|
2023-08-16 03:09:00 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
for ending in (".pyc", ".pyo"):
|
|
|
|
mod = ModuleType(name)
|
|
|
|
pseudopath = tmp_path.joinpath(name + ending)
|
|
|
|
pseudopath.touch()
|
|
|
|
mod.__file__ = str(pseudopath)
|
|
|
|
mp.setitem(sys.modules, name, mod)
|
2024-03-02 22:46:54 +08:00
|
|
|
newmod = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
p, root=tmp_path, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2023-08-16 03:09:00 +08:00
|
|
|
assert mod == newmod
|
2020-06-13 22:29:01 +08:00
|
|
|
mod = ModuleType(name)
|
2020-12-19 21:16:01 +08:00
|
|
|
pseudopath = tmp_path.joinpath(name + "123.py")
|
|
|
|
pseudopath.touch()
|
2020-06-13 22:29:01 +08:00
|
|
|
mod.__file__ = str(pseudopath)
|
|
|
|
monkeypatch.setitem(sys.modules, name, mod)
|
|
|
|
with pytest.raises(ImportPathMismatchError) as excinfo:
|
2024-03-02 23:03:08 +08:00
|
|
|
import_path(p, root=tmp_path, consider_namespace_packages=ns_param)
|
2020-06-13 22:29:01 +08:00
|
|
|
modname, modfile, orig = excinfo.value.args
|
|
|
|
assert modname == name
|
2020-12-19 21:16:01 +08:00
|
|
|
assert modfile == str(pseudopath)
|
2020-06-13 22:29:01 +08:00
|
|
|
assert orig == p
|
|
|
|
assert issubclass(ImportPathMismatchError, ImportError)
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_ensuresyspath_append(self, tmp_path: Path, ns_param: bool) -> None:
|
2020-12-19 21:16:01 +08:00
|
|
|
root1 = tmp_path / "root1"
|
|
|
|
root1.mkdir()
|
|
|
|
file1 = root1 / "x123.py"
|
|
|
|
file1.touch()
|
2020-06-13 22:29:01 +08:00
|
|
|
assert str(root1) not in sys.path
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
file1, mode="append", root=tmp_path, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
assert str(root1) == sys.path[-1]
|
|
|
|
assert str(root1) not in sys.path[:-1]
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_invalid_path(self, tmp_path: Path, ns_param: bool) -> None:
|
2020-06-13 22:29:01 +08:00
|
|
|
with pytest.raises(ImportError):
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
|
|
|
tmp_path / "invalid.py",
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
2023-07-01 23:37:46 +08:00
|
|
|
def simple_module(
|
|
|
|
self, tmp_path: Path, request: pytest.FixtureRequest
|
|
|
|
) -> Iterator[Path]:
|
|
|
|
name = f"mymod_{request.node.name}"
|
|
|
|
fn = tmp_path / f"_src/tests/{name}.py"
|
2021-04-06 04:10:03 +08:00
|
|
|
fn.parent.mkdir(parents=True)
|
2023-06-20 19:55:40 +08:00
|
|
|
fn.write_text("def foo(x): return 40 + x", encoding="utf-8")
|
2023-07-01 23:37:46 +08:00
|
|
|
module_name = module_name_from_path(fn, root=tmp_path)
|
|
|
|
yield fn
|
|
|
|
sys.modules.pop(module_name, None)
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2023-07-01 23:37:46 +08:00
|
|
|
def test_importmode_importlib(
|
2024-03-02 23:03:08 +08:00
|
|
|
self,
|
|
|
|
simple_module: Path,
|
|
|
|
tmp_path: Path,
|
|
|
|
request: pytest.FixtureRequest,
|
|
|
|
ns_param: bool,
|
2023-07-01 23:37:46 +08:00
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`importlib` mode does not change sys.path."""
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
|
|
|
simple_module,
|
|
|
|
mode="importlib",
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert module.foo(2) == 42
|
2020-12-19 21:16:01 +08:00
|
|
|
assert str(simple_module.parent) not in sys.path
|
2021-04-06 04:10:03 +08:00
|
|
|
assert module.__name__ in sys.modules
|
2023-07-01 23:37:46 +08:00
|
|
|
assert module.__name__ == f"_src.tests.mymod_{request.node.name}"
|
2021-04-06 04:10:03 +08:00
|
|
|
assert "_src" in sys.modules
|
|
|
|
assert "_src.tests" in sys.modules
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2023-07-01 23:37:46 +08:00
|
|
|
def test_remembers_previous_imports(
|
2024-03-02 23:03:08 +08:00
|
|
|
self, simple_module: Path, tmp_path: Path, ns_param: bool
|
2021-04-06 04:10:03 +08:00
|
|
|
) -> None:
|
2023-07-01 23:37:46 +08:00
|
|
|
"""`importlib` mode called remembers previous module (#10341, #10811)."""
|
2024-03-02 22:46:54 +08:00
|
|
|
module1 = import_path(
|
|
|
|
simple_module,
|
|
|
|
mode="importlib",
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
|
|
|
module2 = import_path(
|
|
|
|
simple_module,
|
|
|
|
mode="importlib",
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2023-07-01 23:37:46 +08:00
|
|
|
assert module1 is module2
|
2020-06-13 22:29:01 +08:00
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_no_meta_path_found(
|
2024-03-02 23:03:08 +08:00
|
|
|
self,
|
|
|
|
simple_module: Path,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
tmp_path: Path,
|
|
|
|
ns_param: bool,
|
2020-12-19 21:16:01 +08:00
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Even without any meta_path should still import module."""
|
2020-06-13 22:29:01 +08:00
|
|
|
monkeypatch.setattr(sys, "meta_path", [])
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
|
|
|
simple_module,
|
|
|
|
mode="importlib",
|
|
|
|
root=tmp_path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-03-13 21:30:18 +08:00
|
|
|
assert module.foo(2) == 42
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
# mode='importlib' fails if no spec is found to load the module
|
|
|
|
import importlib.util
|
|
|
|
|
2023-07-01 23:37:46 +08:00
|
|
|
# Force module to be re-imported.
|
|
|
|
del sys.modules[module.__name__]
|
|
|
|
|
2020-06-13 22:29:01 +08:00
|
|
|
monkeypatch.setattr(
|
|
|
|
importlib.util, "spec_from_file_location", lambda *args: None
|
|
|
|
)
|
|
|
|
with pytest.raises(ImportError):
|
2024-03-02 22:46:54 +08:00
|
|
|
import_path(
|
|
|
|
simple_module,
|
|
|
|
mode="importlib",
|
|
|
|
root=tmp_path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_resolve_package_path(tmp_path: Path) -> None:
|
2020-06-13 22:29:01 +08:00
|
|
|
pkg = tmp_path / "pkg1"
|
|
|
|
pkg.mkdir()
|
|
|
|
(pkg / "__init__.py").touch()
|
|
|
|
(pkg / "subdir").mkdir()
|
|
|
|
(pkg / "subdir/__init__.py").touch()
|
|
|
|
assert resolve_package_path(pkg) == pkg
|
2023-09-09 20:16:22 +08:00
|
|
|
assert resolve_package_path(pkg / "subdir/__init__.py") == pkg
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_package_unimportable(tmp_path: Path) -> None:
|
2020-06-13 22:29:01 +08:00
|
|
|
pkg = tmp_path / "pkg1-1"
|
|
|
|
pkg.mkdir()
|
|
|
|
pkg.joinpath("__init__.py").touch()
|
2023-09-09 20:16:22 +08:00
|
|
|
subdir = pkg / "subdir"
|
2020-06-13 22:29:01 +08:00
|
|
|
subdir.mkdir()
|
2023-09-09 20:16:22 +08:00
|
|
|
(pkg / "subdir/__init__.py").touch()
|
2020-06-13 22:29:01 +08:00
|
|
|
assert resolve_package_path(subdir) == subdir
|
2023-09-09 20:16:22 +08:00
|
|
|
xyz = subdir / "xyz.py"
|
2020-06-13 22:29:01 +08:00
|
|
|
xyz.touch()
|
|
|
|
assert resolve_package_path(xyz) == subdir
|
|
|
|
assert not resolve_package_path(pkg)
|
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_access_denied_during_cleanup(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
|
2018-10-31 00:31:55 +08:00
|
|
|
"""Ensure that deleting a numbered dir does not fail because of OSErrors (#4262)."""
|
|
|
|
path = tmp_path / "temp-1"
|
|
|
|
path.mkdir()
|
|
|
|
|
|
|
|
def renamed_failed(*args):
|
|
|
|
raise OSError("access denied")
|
|
|
|
|
|
|
|
monkeypatch.setattr(Path, "rename", renamed_failed)
|
|
|
|
|
|
|
|
lock_path = get_lock_path(path)
|
|
|
|
maybe_delete_a_numbered_dir(path)
|
|
|
|
assert not lock_path.is_file()
|
2020-06-02 19:56:33 +08:00
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_long_path_during_cleanup(tmp_path: Path) -> None:
|
2020-06-02 19:56:33 +08:00
|
|
|
"""Ensure that deleting long path works (particularly on Windows (#6775))."""
|
|
|
|
path = (tmp_path / ("a" * 250)).resolve()
|
|
|
|
if sys.platform == "win32":
|
|
|
|
# make sure that the full path is > 260 characters without any
|
|
|
|
# component being over 260 characters
|
|
|
|
assert len(str(path)) > 260
|
|
|
|
extended_path = "\\\\?\\" + str(path)
|
|
|
|
else:
|
|
|
|
extended_path = str(path)
|
|
|
|
os.mkdir(extended_path)
|
|
|
|
assert os.path.isdir(extended_path)
|
|
|
|
maybe_delete_a_numbered_dir(path)
|
|
|
|
assert not os.path.isdir(extended_path)
|
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_get_extended_length_path_str() -> None:
|
2020-06-02 19:56:33 +08:00
|
|
|
assert get_extended_length_path_str(r"c:\foo") == r"\\?\c:\foo"
|
|
|
|
assert get_extended_length_path_str(r"\\share\foo") == r"\\?\UNC\share\foo"
|
|
|
|
assert get_extended_length_path_str(r"\\?\UNC\share\foo") == r"\\?\UNC\share\foo"
|
|
|
|
assert get_extended_length_path_str(r"\\?\c:\foo") == r"\\?\c:\foo"
|
2020-06-12 03:47:59 +08:00
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_suppress_error_removing_lock(tmp_path: Path) -> None:
|
2020-07-15 20:25:17 +08:00
|
|
|
"""ensure_deletable should be resilient if lock file cannot be removed (#5456, #7491)"""
|
2020-06-12 03:47:59 +08:00
|
|
|
path = tmp_path / "dir"
|
|
|
|
path.mkdir()
|
|
|
|
lock = get_lock_path(path)
|
|
|
|
lock.touch()
|
|
|
|
mtime = lock.stat().st_mtime
|
|
|
|
|
2020-07-15 20:25:17 +08:00
|
|
|
with unittest.mock.patch.object(Path, "unlink", side_effect=OSError) as m:
|
2020-06-12 03:47:59 +08:00
|
|
|
assert not ensure_deletable(
|
|
|
|
path, consider_lock_dead_if_created_before=mtime + 30
|
|
|
|
)
|
2020-07-15 20:25:17 +08:00
|
|
|
assert m.call_count == 1
|
|
|
|
assert lock.is_file()
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(Path, "is_file", side_effect=OSError) as m:
|
|
|
|
assert not ensure_deletable(
|
|
|
|
path, consider_lock_dead_if_created_before=mtime + 30
|
|
|
|
)
|
|
|
|
assert m.call_count == 1
|
2020-06-12 03:47:59 +08:00
|
|
|
assert lock.is_file()
|
|
|
|
|
|
|
|
# check now that we can remove the lock file in normal circumstances
|
|
|
|
assert ensure_deletable(path, consider_lock_dead_if_created_before=mtime + 30)
|
|
|
|
assert not lock.is_file()
|
2020-08-04 15:12:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_bestrelpath() -> None:
|
|
|
|
curdir = Path("/foo/bar/baz/path")
|
|
|
|
assert bestrelpath(curdir, curdir) == "."
|
|
|
|
assert bestrelpath(curdir, curdir / "hello" / "world") == "hello" + os.sep + "world"
|
|
|
|
assert bestrelpath(curdir, curdir.parent / "sister") == ".." + os.sep + "sister"
|
|
|
|
assert bestrelpath(curdir, curdir.parent) == ".."
|
|
|
|
assert bestrelpath(curdir, Path("hello")) == "hello"
|
|
|
|
|
|
|
|
|
|
|
|
def test_commonpath() -> None:
|
|
|
|
path = Path("/foo/bar/baz/path")
|
|
|
|
subpath = path / "sampledir"
|
|
|
|
assert commonpath(path, subpath) == path
|
|
|
|
assert commonpath(subpath, path) == path
|
|
|
|
assert commonpath(Path(str(path) + "suffix"), path) == path.parent
|
|
|
|
assert commonpath(path, path.parent.parent) == path.parent.parent
|
2020-10-28 15:27:43 +08:00
|
|
|
|
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
def test_visit_ignores_errors(tmp_path: Path) -> None:
|
|
|
|
symlink_or_skip("recursive", tmp_path / "recursive")
|
|
|
|
tmp_path.joinpath("foo").write_bytes(b"")
|
|
|
|
tmp_path.joinpath("bar").write_bytes(b"")
|
2020-10-28 15:27:43 +08:00
|
|
|
|
2020-12-19 21:16:01 +08:00
|
|
|
assert [
|
|
|
|
entry.name for entry in visit(str(tmp_path), recurse=lambda entry: False)
|
|
|
|
] == ["bar", "foo"]
|
2020-12-12 19:49:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only")
|
|
|
|
def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
"""
|
|
|
|
import_file() should not raise ImportPathMismatchError if the paths are exactly
|
|
|
|
equal on Windows. It seems directories mounted as UNC paths make os.path.samefile
|
|
|
|
return False, even when they are clearly equal.
|
|
|
|
"""
|
|
|
|
module_path = tmp_path.joinpath("my_module.py")
|
2023-06-20 19:55:40 +08:00
|
|
|
module_path.write_text("def foo(): return 42", encoding="utf-8")
|
2020-12-12 19:49:58 +08:00
|
|
|
monkeypatch.syspath_prepend(tmp_path)
|
|
|
|
|
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
# Forcibly make os.path.samefile() return False here to ensure we are comparing
|
|
|
|
# the paths too. Using a context to narrow the patch as much as possible given
|
|
|
|
# this is an important system function.
|
|
|
|
mp.setattr(os.path, "samefile", lambda x, y: False)
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
|
|
|
module_path, root=tmp_path, consider_namespace_packages=False
|
|
|
|
)
|
2020-12-12 19:49:58 +08:00
|
|
|
assert getattr(module, "foo")() == 42
|
2021-04-06 04:10:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestImportLibMode:
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_importmode_importlib_with_dataclass(
|
|
|
|
self, tmp_path: Path, ns_param: bool
|
|
|
|
) -> None:
|
2021-04-06 04:10:03 +08:00
|
|
|
"""Ensure that importlib mode works with a module containing dataclasses (#7856)."""
|
|
|
|
fn = tmp_path.joinpath("_src/tests/test_dataclass.py")
|
|
|
|
fn.parent.mkdir(parents=True)
|
|
|
|
fn.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Data:
|
|
|
|
value: str
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2021-04-06 04:10:03 +08:00
|
|
|
)
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
fn, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2021-04-06 04:10:03 +08:00
|
|
|
Data: Any = getattr(module, "Data")
|
|
|
|
data = Data(value="foo")
|
|
|
|
assert data.value == "foo"
|
|
|
|
assert data.__module__ == "_src.tests.test_dataclass"
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
module2 = import_path(
|
|
|
|
fn, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
|
|
|
|
)
|
|
|
|
assert module is module2
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_importmode_importlib_with_pickle(
|
|
|
|
self, tmp_path: Path, ns_param: bool
|
|
|
|
) -> None:
|
2021-04-06 04:10:03 +08:00
|
|
|
"""Ensure that importlib mode works with pickle (#7859)."""
|
|
|
|
fn = tmp_path.joinpath("_src/tests/test_pickle.py")
|
|
|
|
fn.parent.mkdir(parents=True)
|
|
|
|
fn.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
def _action():
|
|
|
|
return 42
|
|
|
|
|
|
|
|
def round_trip():
|
|
|
|
s = pickle.dumps(_action)
|
|
|
|
return pickle.loads(s)
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2021-04-06 04:10:03 +08:00
|
|
|
)
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
fn, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2021-04-06 04:10:03 +08:00
|
|
|
round_trip = getattr(module, "round_trip")
|
|
|
|
action = round_trip()
|
|
|
|
assert action() == 42
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
module2 = import_path(
|
|
|
|
fn, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
|
|
|
|
)
|
|
|
|
assert module is module2
|
|
|
|
|
2021-04-06 04:10:03 +08:00
|
|
|
def test_importmode_importlib_with_pickle_separate_modules(
|
2024-03-02 23:03:08 +08:00
|
|
|
self, tmp_path: Path, ns_param: bool
|
2021-04-06 04:10:03 +08:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Ensure that importlib mode works can load pickles that look similar but are
|
|
|
|
defined in separate modules.
|
|
|
|
"""
|
|
|
|
fn1 = tmp_path.joinpath("_src/m1/tests/test.py")
|
|
|
|
fn1.parent.mkdir(parents=True)
|
|
|
|
fn1.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
2023-01-20 17:13:36 +08:00
|
|
|
import dataclasses
|
2021-04-06 04:10:03 +08:00
|
|
|
import pickle
|
|
|
|
|
2023-01-20 17:13:36 +08:00
|
|
|
@dataclasses.dataclass
|
2021-04-06 04:10:03 +08:00
|
|
|
class Data:
|
|
|
|
x: int = 42
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2021-04-06 04:10:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
fn2 = tmp_path.joinpath("_src/m2/tests/test.py")
|
|
|
|
fn2.parent.mkdir(parents=True)
|
|
|
|
fn2.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
2023-01-20 17:13:36 +08:00
|
|
|
import dataclasses
|
2021-04-06 04:10:03 +08:00
|
|
|
import pickle
|
|
|
|
|
2023-01-20 17:13:36 +08:00
|
|
|
@dataclasses.dataclass
|
2021-04-06 04:10:03 +08:00
|
|
|
class Data:
|
|
|
|
x: str = ""
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2021-04-06 04:10:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
def round_trip(obj):
|
|
|
|
s = pickle.dumps(obj)
|
|
|
|
return pickle.loads(s)
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
fn1, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2021-04-06 04:10:03 +08:00
|
|
|
Data1 = getattr(module, "Data")
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
module = import_path(
|
2024-03-02 23:03:08 +08:00
|
|
|
fn2, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2021-04-06 04:10:03 +08:00
|
|
|
Data2 = getattr(module, "Data")
|
|
|
|
|
|
|
|
assert round_trip(Data1(20)) == Data1(20)
|
|
|
|
assert round_trip(Data2("hello")) == Data2("hello")
|
|
|
|
assert Data1.__module__ == "_src.m1.tests.test"
|
|
|
|
assert Data2.__module__ == "_src.m2.tests.test"
|
|
|
|
|
|
|
|
def test_module_name_from_path(self, tmp_path: Path) -> None:
|
|
|
|
result = module_name_from_path(tmp_path / "src/tests/test_foo.py", tmp_path)
|
|
|
|
assert result == "src.tests.test_foo"
|
|
|
|
|
|
|
|
# Path is not relative to root dir: use the full path to obtain the module name.
|
|
|
|
result = module_name_from_path(Path("/home/foo/test_foo.py"), Path("/bar"))
|
|
|
|
assert result == "home.foo.test_foo"
|
|
|
|
|
2023-09-06 06:42:40 +08:00
|
|
|
# Importing __init__.py files should return the package as module name.
|
|
|
|
result = module_name_from_path(tmp_path / "src/app/__init__.py", tmp_path)
|
|
|
|
assert result == "src.app"
|
|
|
|
|
2023-09-10 20:57:40 +08:00
|
|
|
# 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__"
|
|
|
|
|
2024-02-28 02:08:15 +08:00
|
|
|
# Modules which start with "." are considered relative and will not be imported
|
|
|
|
# unless part of a package, so we replace it with a "_" when generating the fake module name.
|
|
|
|
result = module_name_from_path(tmp_path / ".env/tests/test_foo.py", tmp_path)
|
|
|
|
assert result == "_env.tests.test_foo"
|
|
|
|
|
|
|
|
# We want to avoid generating extra intermediate modules if some directory just happens
|
|
|
|
# to contain a "." in the name.
|
|
|
|
result = module_name_from_path(
|
|
|
|
tmp_path / ".env.310/tests/test_foo.py", tmp_path
|
|
|
|
)
|
|
|
|
assert result == "_env_310.tests.test_foo"
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
def test_resolve_pkg_root_and_module_name(
|
2024-04-10 00:21:51 +08:00
|
|
|
self, tmp_path: Path, monkeypatch: MonkeyPatch, pytester: Pytester
|
2024-02-28 02:13:28 +08:00
|
|
|
) -> None:
|
|
|
|
# Create a directory structure first without __init__.py files.
|
|
|
|
(tmp_path / "src/app/core").mkdir(parents=True)
|
|
|
|
models_py = tmp_path / "src/app/core/models.py"
|
|
|
|
models_py.touch()
|
2024-04-10 00:21:51 +08:00
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
with pytest.raises(CouldNotResolvePathError):
|
|
|
|
_ = resolve_pkg_root_and_module_name(models_py)
|
|
|
|
|
|
|
|
# Create the __init__.py files, it should now resolve to a proper module name.
|
|
|
|
(tmp_path / "src/app/__init__.py").touch()
|
|
|
|
(tmp_path / "src/app/core/__init__.py").touch()
|
2024-03-02 23:03:08 +08:00
|
|
|
assert resolve_pkg_root_and_module_name(
|
|
|
|
models_py, consider_namespace_packages=True
|
|
|
|
) == (
|
2024-02-28 02:13:28 +08:00
|
|
|
tmp_path / "src",
|
|
|
|
"app.core.models",
|
|
|
|
)
|
|
|
|
|
|
|
|
# If we add tmp_path to sys.path, src becomes a namespace package.
|
|
|
|
monkeypatch.syspath_prepend(tmp_path)
|
2024-04-10 00:21:51 +08:00
|
|
|
validate_namespace_package(pytester, [tmp_path], ["src.app.core.models"])
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
assert resolve_pkg_root_and_module_name(
|
2024-03-02 22:46:54 +08:00
|
|
|
models_py, consider_namespace_packages=True
|
2024-02-28 02:13:28 +08:00
|
|
|
) == (
|
|
|
|
tmp_path,
|
|
|
|
"src.app.core.models",
|
|
|
|
)
|
2024-03-02 23:03:08 +08:00
|
|
|
assert resolve_pkg_root_and_module_name(
|
|
|
|
models_py, consider_namespace_packages=False
|
|
|
|
) == (
|
|
|
|
tmp_path / "src",
|
|
|
|
"app.core.models",
|
|
|
|
)
|
2024-02-28 02:13:28 +08:00
|
|
|
|
2022-02-13 20:39:57 +08:00
|
|
|
def test_insert_missing_modules(
|
|
|
|
self, monkeypatch: MonkeyPatch, tmp_path: Path
|
|
|
|
) -> None:
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
# Use 'xxx' and 'xxy' as parent names as they are unlikely to exist and
|
|
|
|
# don't end up being imported.
|
|
|
|
modules = {"xxx.tests.foo": ModuleType("xxx.tests.foo")}
|
|
|
|
insert_missing_modules(modules, "xxx.tests.foo")
|
|
|
|
assert sorted(modules) == ["xxx", "xxx.tests", "xxx.tests.foo"]
|
2021-04-06 04:10:03 +08:00
|
|
|
|
|
|
|
mod = ModuleType("mod", doc="My Module")
|
2022-02-13 20:39:57 +08:00
|
|
|
modules = {"xxy": mod}
|
|
|
|
insert_missing_modules(modules, "xxy")
|
|
|
|
assert modules == {"xxy": mod}
|
2021-04-06 04:10:03 +08:00
|
|
|
|
|
|
|
modules = {}
|
|
|
|
insert_missing_modules(modules, "")
|
|
|
|
assert modules == {}
|
2023-07-01 23:12:41 +08:00
|
|
|
|
|
|
|
def test_parent_contains_child_module_attribute(
|
|
|
|
self, monkeypatch: MonkeyPatch, tmp_path: Path
|
|
|
|
):
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
# Use 'xxx' and 'xxy' as parent names as they are unlikely to exist and
|
|
|
|
# don't end up being imported.
|
|
|
|
modules = {"xxx.tests.foo": ModuleType("xxx.tests.foo")}
|
|
|
|
insert_missing_modules(modules, "xxx.tests.foo")
|
|
|
|
assert sorted(modules) == ["xxx", "xxx.tests", "xxx.tests.foo"]
|
|
|
|
assert modules["xxx"].tests is modules["xxx.tests"]
|
|
|
|
assert modules["xxx.tests"].foo is modules["xxx.tests.foo"]
|
2023-09-06 06:42:40 +08:00
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_importlib_package(
|
|
|
|
self, monkeypatch: MonkeyPatch, tmp_path: Path, ns_param: bool
|
|
|
|
):
|
2023-09-06 06:42:40 +08:00
|
|
|
"""
|
|
|
|
Importing a package using --importmode=importlib should not import the
|
|
|
|
package's __init__.py file more than once (#11306).
|
|
|
|
"""
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
monkeypatch.syspath_prepend(tmp_path)
|
|
|
|
|
|
|
|
package_name = "importlib_import_package"
|
|
|
|
tmp_path.joinpath(package_name).mkdir()
|
|
|
|
init = tmp_path.joinpath(f"{package_name}/__init__.py")
|
|
|
|
init.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
from .singleton import Singleton
|
|
|
|
|
|
|
|
instance = Singleton()
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
singleton = tmp_path.joinpath(f"{package_name}/singleton.py")
|
|
|
|
singleton.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
class Singleton:
|
|
|
|
INSTANCES = []
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.INSTANCES.append(self)
|
|
|
|
if len(self.INSTANCES) > 1:
|
|
|
|
raise RuntimeError("Already initialized")
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
|
|
|
init,
|
|
|
|
root=tmp_path,
|
|
|
|
mode=ImportMode.importlib,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2023-09-06 06:42:40 +08:00
|
|
|
assert len(mod.instance.INSTANCES) == 1
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
mod2 = import_path(
|
|
|
|
init,
|
|
|
|
root=tmp_path,
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
consider_namespace_packages=ns_param,
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
2023-09-07 23:49:25 +08:00
|
|
|
|
2023-09-10 20:57:40 +08:00
|
|
|
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 *")
|
|
|
|
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
def create_installed_doctests_and_tests_dir(
|
|
|
|
self, path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> Tuple[Path, Path, Path]:
|
|
|
|
"""
|
|
|
|
Create a directory structure where the application code is installed in a virtual environment,
|
|
|
|
and the tests are in an outside ".tests" directory.
|
|
|
|
|
|
|
|
Return the paths to the core module (installed in the virtualenv), and the test modules.
|
|
|
|
"""
|
|
|
|
app = path / "src/app"
|
|
|
|
app.mkdir(parents=True)
|
|
|
|
(app / "__init__.py").touch()
|
|
|
|
core_py = app / "core.py"
|
|
|
|
core_py.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
def foo():
|
|
|
|
'''
|
|
|
|
>>> 1 + 1
|
|
|
|
2
|
|
|
|
'''
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Install it into a site-packages directory, and add it to sys.path, mimicking what
|
|
|
|
# happens when installing into a virtualenv.
|
|
|
|
site_packages = path / ".env/lib/site-packages"
|
|
|
|
site_packages.mkdir(parents=True)
|
|
|
|
shutil.copytree(app, site_packages / "app")
|
|
|
|
assert (site_packages / "app/core.py").is_file()
|
|
|
|
|
|
|
|
monkeypatch.syspath_prepend(site_packages)
|
|
|
|
|
|
|
|
# Create the tests files, outside 'src' and the virtualenv.
|
|
|
|
# We use the same test name on purpose, but in different directories, to ensure
|
|
|
|
# this works as advertised.
|
|
|
|
conftest_path1 = path / ".tests/a/conftest.py"
|
|
|
|
conftest_path1.parent.mkdir(parents=True)
|
|
|
|
conftest_path1.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def a_fix(): return "a"
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
test_path1 = path / ".tests/a/test_core.py"
|
|
|
|
test_path1.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
import app.core
|
|
|
|
def test(a_fix):
|
|
|
|
assert a_fix == "a"
|
|
|
|
""",
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
|
|
|
conftest_path2 = path / ".tests/b/conftest.py"
|
|
|
|
conftest_path2.parent.mkdir(parents=True)
|
|
|
|
conftest_path2.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def b_fix(): return "b"
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
|
|
|
|
test_path2 = path / ".tests/b/test_core.py"
|
|
|
|
test_path2.write_text(
|
|
|
|
dedent(
|
|
|
|
"""
|
|
|
|
import app.core
|
|
|
|
def test(b_fix):
|
|
|
|
assert b_fix == "b"
|
|
|
|
""",
|
|
|
|
),
|
|
|
|
encoding="ascii",
|
|
|
|
)
|
|
|
|
return (site_packages / "app/core.py"), test_path1, test_path2
|
|
|
|
|
|
|
|
def test_import_using_normal_mechanism_first(
|
2024-03-02 23:03:08 +08:00
|
|
|
self, monkeypatch: MonkeyPatch, pytester: Pytester, ns_param: bool
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Test import_path imports from the canonical location when possible first, only
|
|
|
|
falling back to its normal flow when the module being imported is not reachable via sys.path (#11475).
|
|
|
|
"""
|
|
|
|
core_py, test_path1, test_path2 = self.create_installed_doctests_and_tests_dir(
|
|
|
|
pytester.path, monkeypatch
|
|
|
|
)
|
|
|
|
|
|
|
|
# core_py is reached from sys.path, so should be imported normally.
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
|
|
|
core_py,
|
|
|
|
mode="importlib",
|
|
|
|
root=pytester.path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
assert mod.__name__ == "app.core"
|
|
|
|
assert mod.__file__ and Path(mod.__file__) == core_py
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
mod2 = import_path(
|
|
|
|
core_py,
|
|
|
|
mode="importlib",
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=ns_param,
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
|
|
|
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
# tests are not reachable from sys.path, so they are imported as a standalone modules.
|
|
|
|
# Instead of '.tests.a.test_core', we import as "_tests.a.test_core" because
|
|
|
|
# importlib considers module names starting with '.' to be local imports.
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
|
|
|
test_path1,
|
|
|
|
mode="importlib",
|
|
|
|
root=pytester.path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
assert mod.__name__ == "_tests.a.test_core"
|
2024-03-04 23:44:56 +08:00
|
|
|
|
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
mod2 = import_path(
|
|
|
|
test_path1,
|
|
|
|
mode="importlib",
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=ns_param,
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
|
|
|
test_path2,
|
|
|
|
mode="importlib",
|
|
|
|
root=pytester.path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
assert mod.__name__ == "_tests.b.test_core"
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
mod2 = import_path(
|
|
|
|
test_path2,
|
|
|
|
mode="importlib",
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=ns_param,
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
|
|
|
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
def test_import_using_normal_mechanism_first_integration(
|
2024-03-02 23:03:08 +08:00
|
|
|
self, monkeypatch: MonkeyPatch, pytester: Pytester, ns_param: bool
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Same test as above, but verify the behavior calling pytest.
|
|
|
|
|
|
|
|
We should not make this call in the same test as above, as the modules have already
|
|
|
|
been imported by separate import_path() calls.
|
|
|
|
"""
|
|
|
|
core_py, test_path1, test_path2 = self.create_installed_doctests_and_tests_dir(
|
|
|
|
pytester.path, monkeypatch
|
|
|
|
)
|
|
|
|
result = pytester.runpytest(
|
|
|
|
"--import-mode=importlib",
|
2024-03-02 23:03:08 +08:00
|
|
|
"-o",
|
|
|
|
f"consider_namespace_packages={ns_param}",
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
"--doctest-modules",
|
|
|
|
"--pyargs",
|
|
|
|
"app",
|
|
|
|
"./.tests",
|
|
|
|
)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
f"{core_py.relative_to(pytester.path)} . *",
|
|
|
|
f"{test_path1.relative_to(pytester.path)} . *",
|
|
|
|
f"{test_path2.relative_to(pytester.path)} . *",
|
|
|
|
"* 3 passed*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2024-03-02 23:03:08 +08:00
|
|
|
def test_import_path_imports_correct_file(
|
|
|
|
self, pytester: Pytester, ns_param: bool
|
|
|
|
) -> None:
|
2024-02-28 03:59:02 +08:00
|
|
|
"""
|
|
|
|
Import the module by the given path, even if other module with the same name
|
|
|
|
is reachable from sys.path.
|
|
|
|
"""
|
|
|
|
pytester.syspathinsert()
|
|
|
|
# Create a 'x.py' module reachable from sys.path that raises AssertionError
|
|
|
|
# if imported.
|
|
|
|
x_at_root = pytester.path / "x.py"
|
|
|
|
x_at_root.write_text("raise AssertionError('x at root')", encoding="ascii")
|
|
|
|
|
|
|
|
# Create another x.py module, but in some subdirectories to ensure it is not
|
|
|
|
# accessible from sys.path.
|
|
|
|
x_in_sub_folder = pytester.path / "a/b/x.py"
|
|
|
|
x_in_sub_folder.parent.mkdir(parents=True)
|
|
|
|
x_in_sub_folder.write_text("X = 'a/b/x'", encoding="ascii")
|
|
|
|
|
|
|
|
# Import our x.py module from the subdirectories.
|
|
|
|
# The 'x.py' module from sys.path was not imported for sure because
|
|
|
|
# otherwise we would get an AssertionError.
|
|
|
|
mod = import_path(
|
2024-03-02 22:46:54 +08:00
|
|
|
x_in_sub_folder,
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-02-28 03:59:02 +08:00
|
|
|
)
|
|
|
|
assert mod.__file__ and Path(mod.__file__) == x_in_sub_folder
|
|
|
|
assert mod.X == "a/b/x"
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
mod2 = import_path(
|
|
|
|
x_in_sub_folder,
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=ns_param,
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
|
|
|
|
2024-02-28 03:59:02 +08:00
|
|
|
# Attempt to import root 'x.py'.
|
|
|
|
with pytest.raises(AssertionError, match="x at root"):
|
2024-03-02 22:46:54 +08:00
|
|
|
_ = import_path(
|
|
|
|
x_at_root,
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
2024-03-02 23:03:08 +08:00
|
|
|
consider_namespace_packages=ns_param,
|
2024-03-02 22:46:54 +08:00
|
|
|
)
|
2024-02-28 03:59:02 +08:00
|
|
|
|
2023-09-07 23:49:25 +08:00
|
|
|
|
|
|
|
def test_safe_exists(tmp_path: Path) -> None:
|
|
|
|
d = tmp_path.joinpath("some_dir")
|
|
|
|
d.mkdir()
|
|
|
|
assert safe_exists(d) is True
|
|
|
|
|
|
|
|
f = tmp_path.joinpath("some_file")
|
|
|
|
f.touch()
|
|
|
|
assert safe_exists(f) is True
|
|
|
|
|
|
|
|
# Use unittest.mock() as a context manager to have a very narrow
|
|
|
|
# patch lifetime.
|
|
|
|
p = tmp_path.joinpath("some long filename" * 100)
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
Path,
|
|
|
|
"exists",
|
|
|
|
autospec=True,
|
|
|
|
side_effect=OSError(errno.ENAMETOOLONG, "name too long"),
|
|
|
|
):
|
|
|
|
assert safe_exists(p) is False
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
Path,
|
|
|
|
"exists",
|
|
|
|
autospec=True,
|
2023-09-08 02:44:47 +08:00
|
|
|
side_effect=ValueError("name too long"),
|
2023-09-07 23:49:25 +08:00
|
|
|
):
|
2023-09-08 02:44:47 +08:00
|
|
|
assert safe_exists(p) is False
|
2024-02-28 02:13:28 +08:00
|
|
|
|
|
|
|
|
2024-04-20 19:31:33 +08:00
|
|
|
def test_import_sets_module_as_attribute(pytester: Pytester) -> None:
|
|
|
|
"""Unittest test for #12194."""
|
|
|
|
pytester.path.joinpath("foo/bar/baz").mkdir(parents=True)
|
|
|
|
pytester.path.joinpath("foo/__init__.py").touch()
|
|
|
|
pytester.path.joinpath("foo/bar/__init__.py").touch()
|
|
|
|
pytester.path.joinpath("foo/bar/baz/__init__.py").touch()
|
|
|
|
pytester.syspathinsert()
|
|
|
|
|
|
|
|
# Import foo.bar.baz and ensure parent modules also ended up imported.
|
|
|
|
baz = import_path(
|
|
|
|
pytester.path.joinpath("foo/bar/baz/__init__.py"),
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
|
|
|
assert baz.__name__ == "foo.bar.baz"
|
|
|
|
foo = sys.modules["foo"]
|
|
|
|
assert foo.__name__ == "foo"
|
|
|
|
bar = sys.modules["foo.bar"]
|
|
|
|
assert bar.__name__ == "foo.bar"
|
|
|
|
|
|
|
|
# Check parent modules have an attribute pointing to their children.
|
|
|
|
assert bar.baz is baz
|
|
|
|
assert foo.bar is bar
|
|
|
|
|
|
|
|
# Ensure we returned the "foo.bar" module cached in sys.modules.
|
|
|
|
bar_2 = import_path(
|
|
|
|
pytester.path.joinpath("foo/bar/__init__.py"),
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
|
|
|
assert bar_2 is bar
|
|
|
|
|
|
|
|
|
|
|
|
def test_import_sets_module_as_attribute_without_init_files(pytester: Pytester) -> None:
|
|
|
|
"""Similar to test_import_sets_module_as_attribute, but without __init__.py files."""
|
|
|
|
pytester.path.joinpath("foo/bar").mkdir(parents=True)
|
|
|
|
pytester.path.joinpath("foo/bar/baz.py").touch()
|
|
|
|
pytester.syspathinsert()
|
|
|
|
|
|
|
|
# Import foo.bar.baz and ensure parent modules also ended up imported.
|
|
|
|
baz = import_path(
|
|
|
|
pytester.path.joinpath("foo/bar/baz.py"),
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
|
|
|
assert baz.__name__ == "foo.bar.baz"
|
|
|
|
foo = sys.modules["foo"]
|
|
|
|
assert foo.__name__ == "foo"
|
|
|
|
bar = sys.modules["foo.bar"]
|
|
|
|
assert bar.__name__ == "foo.bar"
|
|
|
|
|
|
|
|
# Check parent modules have an attribute pointing to their children.
|
|
|
|
assert bar.baz is baz
|
|
|
|
assert foo.bar is bar
|
|
|
|
|
|
|
|
# Ensure we returned the "foo.bar.baz" module cached in sys.modules.
|
|
|
|
baz_2 = import_path(
|
|
|
|
pytester.path.joinpath("foo/bar/baz.py"),
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
|
|
|
assert baz_2 is baz
|
|
|
|
|
|
|
|
|
|
|
|
def test_import_sets_module_as_attribute_regression(pytester: Pytester) -> None:
|
|
|
|
"""Regression test for #12194."""
|
|
|
|
pytester.path.joinpath("foo/bar/baz").mkdir(parents=True)
|
|
|
|
pytester.path.joinpath("foo/__init__.py").touch()
|
|
|
|
pytester.path.joinpath("foo/bar/__init__.py").touch()
|
|
|
|
pytester.path.joinpath("foo/bar/baz/__init__.py").touch()
|
|
|
|
f = pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import foo
|
|
|
|
from foo.bar import baz
|
|
|
|
foo.bar.baz
|
|
|
|
|
|
|
|
def test_foo() -> None:
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
pytester.syspathinsert()
|
|
|
|
result = pytester.runpython(f)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
result = pytester.runpytest("--import-mode=importlib", "--doctest-modules")
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_import_submodule_not_namespace(pytester: Pytester) -> None:
|
|
|
|
"""
|
|
|
|
Regression test for importing a submodule 'foo.bar' while there is a 'bar' directory
|
|
|
|
reachable from sys.path -- ensuring the top-level module does not end up imported as a namespace
|
|
|
|
package.
|
|
|
|
|
|
|
|
#12194
|
|
|
|
https://github.com/pytest-dev/pytest/pull/12208#issuecomment-2056458432
|
|
|
|
"""
|
|
|
|
pytester.syspathinsert()
|
|
|
|
# Create package 'foo' with a submodule 'bar'.
|
|
|
|
pytester.path.joinpath("foo").mkdir()
|
|
|
|
foo_path = pytester.path.joinpath("foo/__init__.py")
|
|
|
|
foo_path.touch()
|
|
|
|
bar_path = pytester.path.joinpath("foo/bar.py")
|
|
|
|
bar_path.touch()
|
|
|
|
# Create top-level directory in `sys.path` with the same name as that submodule.
|
|
|
|
pytester.path.joinpath("bar").mkdir()
|
|
|
|
|
|
|
|
# Import `foo`, then `foo.bar`, and check they were imported from the correct location.
|
|
|
|
foo = import_path(
|
|
|
|
foo_path,
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
|
|
|
bar = import_path(
|
|
|
|
bar_path,
|
|
|
|
mode=ImportMode.importlib,
|
|
|
|
root=pytester.path,
|
|
|
|
consider_namespace_packages=False,
|
|
|
|
)
|
|
|
|
assert foo.__name__ == "foo"
|
|
|
|
assert bar.__name__ == "foo.bar"
|
|
|
|
assert foo.__file__ is not None
|
|
|
|
assert bar.__file__ is not None
|
|
|
|
assert Path(foo.__file__) == foo_path
|
|
|
|
assert Path(bar.__file__) == bar_path
|
|
|
|
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
class TestNamespacePackages:
|
|
|
|
"""Test import_path support when importing from properly namespace packages."""
|
|
|
|
|
2024-04-10 00:21:51 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_imports_tracking(self, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
monkeypatch.setattr(sys, "pytest_namespace_packages_test", [], raising=False)
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
def setup_directories(
|
2024-04-10 00:21:51 +08:00
|
|
|
self, tmp_path: Path, monkeypatch: Optional[MonkeyPatch], pytester: Pytester
|
2024-02-28 02:13:28 +08:00
|
|
|
) -> Tuple[Path, Path]:
|
2024-04-10 00:21:51 +08:00
|
|
|
# Use a code to guard against modules being imported more than once.
|
|
|
|
# This is a safeguard in case future changes break this invariant.
|
|
|
|
code = dedent(
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
imported = getattr(sys, "pytest_namespace_packages_test", [])
|
|
|
|
assert __name__ not in imported, f"{__name__} already imported"
|
|
|
|
imported.append(__name__)
|
|
|
|
sys.pytest_namespace_packages_test = imported
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
# Set up a namespace package "com.company", containing
|
|
|
|
# two subpackages, "app" and "calc".
|
|
|
|
(tmp_path / "src/dist1/com/company/app/core").mkdir(parents=True)
|
2024-04-10 00:21:51 +08:00
|
|
|
(tmp_path / "src/dist1/com/company/app/__init__.py").write_text(
|
|
|
|
code, encoding="UTF-8"
|
|
|
|
)
|
|
|
|
(tmp_path / "src/dist1/com/company/app/core/__init__.py").write_text(
|
|
|
|
code, encoding="UTF-8"
|
|
|
|
)
|
2024-02-28 02:13:28 +08:00
|
|
|
models_py = tmp_path / "src/dist1/com/company/app/core/models.py"
|
|
|
|
models_py.touch()
|
|
|
|
|
|
|
|
(tmp_path / "src/dist2/com/company/calc/algo").mkdir(parents=True)
|
2024-04-10 00:21:51 +08:00
|
|
|
(tmp_path / "src/dist2/com/company/calc/__init__.py").write_text(
|
|
|
|
code, encoding="UTF-8"
|
|
|
|
)
|
|
|
|
(tmp_path / "src/dist2/com/company/calc/algo/__init__.py").write_text(
|
|
|
|
code, encoding="UTF-8"
|
|
|
|
)
|
2024-02-28 02:13:28 +08:00
|
|
|
algorithms_py = tmp_path / "src/dist2/com/company/calc/algo/algorithms.py"
|
2024-04-10 00:21:51 +08:00
|
|
|
algorithms_py.write_text(code, encoding="UTF-8")
|
2024-02-28 02:13:28 +08:00
|
|
|
|
2024-04-10 00:21:51 +08:00
|
|
|
r = validate_namespace_package(
|
|
|
|
pytester,
|
|
|
|
[tmp_path / "src/dist1", tmp_path / "src/dist2"],
|
|
|
|
["com.company.app.core.models", "com.company.calc.algo.algorithms"],
|
2024-02-28 02:13:28 +08:00
|
|
|
)
|
|
|
|
assert r.ret == 0
|
2024-04-10 00:21:51 +08:00
|
|
|
if monkeypatch is not None:
|
|
|
|
monkeypatch.syspath_prepend(tmp_path / "src/dist1")
|
|
|
|
monkeypatch.syspath_prepend(tmp_path / "src/dist2")
|
2024-02-28 02:13:28 +08:00
|
|
|
return models_py, algorithms_py
|
|
|
|
|
Change importlib to first try to import modules using the standard mechanism
As detailed in
https://github.com/pytest-dev/pytest/issues/11475#issuecomment-1937043670,
currently with `--import-mode=importlib` pytest will try to import every
file by using a unique module name, regardless if that module could be
imported using the normal import mechanism without touching `sys.path`.
This has the consequence that non-test modules available in `sys.path`
(via other mechanism, such as being installed into a virtualenv,
PYTHONPATH, etc) would end up being imported as standalone modules,
instead of imported with their expected module names.
To illustrate:
```
.env/
lib/
site-packages/
anndata/
core.py
```
Given `anndata` is installed into the virtual environment, `python -c
"import anndata.core"` works, but pytest with `importlib` mode would
import that module as a standalone module named
`".env.lib.site-packages.anndata.core"`, because importlib module was
designed to import test files which are not reachable from `sys.path`,
but now it is clear that normal modules should be imported using the
standard mechanisms if possible.
Now `imporlib` mode will first try to import the module normally,
without changing `sys.path`, and if that fails it falls back to
importing the module as a standalone module.
This also makes `importlib` respect namespace packages.
This supersedes #11931.
Fix #11475
Close #11931
2024-02-17 21:39:15 +08:00
|
|
|
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
2024-02-28 02:13:28 +08:00
|
|
|
def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
|
|
|
|
self,
|
|
|
|
tmp_path: Path,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
pytester: Pytester,
|
|
|
|
import_mode: str,
|
|
|
|
) -> None:
|
|
|
|
models_py, algorithms_py = self.setup_directories(
|
|
|
|
tmp_path, monkeypatch, pytester
|
|
|
|
)
|
|
|
|
|
|
|
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
2024-03-02 22:46:54 +08:00
|
|
|
models_py, consider_namespace_packages=True
|
2024-02-28 02:13:28 +08:00
|
|
|
)
|
|
|
|
assert (pkg_root, module_name) == (
|
|
|
|
tmp_path / "src/dist1",
|
|
|
|
"com.company.app.core.models",
|
|
|
|
)
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
|
|
|
models_py, mode=import_mode, root=tmp_path, consider_namespace_packages=True
|
|
|
|
)
|
2024-02-28 02:13:28 +08:00
|
|
|
assert mod.__name__ == "com.company.app.core.models"
|
|
|
|
assert mod.__file__ == str(models_py)
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
mod2 = import_path(
|
|
|
|
models_py, mode=import_mode, root=tmp_path, consider_namespace_packages=True
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
2024-03-02 22:46:54 +08:00
|
|
|
algorithms_py, consider_namespace_packages=True
|
2024-02-28 02:13:28 +08:00
|
|
|
)
|
|
|
|
assert (pkg_root, module_name) == (
|
|
|
|
tmp_path / "src/dist2",
|
|
|
|
"com.company.calc.algo.algorithms",
|
|
|
|
)
|
|
|
|
|
2024-03-02 22:46:54 +08:00
|
|
|
mod = import_path(
|
|
|
|
algorithms_py,
|
|
|
|
mode=import_mode,
|
|
|
|
root=tmp_path,
|
|
|
|
consider_namespace_packages=True,
|
|
|
|
)
|
2024-02-28 02:13:28 +08:00
|
|
|
assert mod.__name__ == "com.company.calc.algo.algorithms"
|
|
|
|
assert mod.__file__ == str(algorithms_py)
|
|
|
|
|
2024-03-04 23:44:56 +08:00
|
|
|
# Ensure we do not import the same module again (#11475).
|
|
|
|
mod2 = import_path(
|
|
|
|
algorithms_py,
|
|
|
|
mode=import_mode,
|
|
|
|
root=tmp_path,
|
|
|
|
consider_namespace_packages=True,
|
|
|
|
)
|
|
|
|
assert mod is mod2
|
|
|
|
|
2024-02-28 02:13:28 +08:00
|
|
|
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
|
|
|
def test_incorrect_namespace_package(
|
|
|
|
self,
|
|
|
|
tmp_path: Path,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
pytester: Pytester,
|
|
|
|
import_mode: str,
|
|
|
|
) -> None:
|
|
|
|
models_py, algorithms_py = self.setup_directories(
|
|
|
|
tmp_path, monkeypatch, pytester
|
|
|
|
)
|
2024-04-10 00:21:51 +08:00
|
|
|
# Namespace packages must not have an __init__.py at its top-level
|
|
|
|
# directory; if it does, it is no longer a namespace package, and we fall back
|
|
|
|
# to importing just the part of the package containing the __init__.py files.
|
2024-02-28 02:13:28 +08:00
|
|
|
(tmp_path / "src/dist1/com/__init__.py").touch()
|
|
|
|
|
2024-04-10 00:21:51 +08:00
|
|
|
# Because of the __init__ file, 'com' is no longer a namespace package:
|
|
|
|
# 'com.company.app' is importable as a normal module.
|
|
|
|
# 'com.company.calc' is no longer importable because 'com' is not a namespace package anymore.
|
|
|
|
r = validate_namespace_package(
|
|
|
|
pytester,
|
|
|
|
[tmp_path / "src/dist1", tmp_path / "src/dist2"],
|
|
|
|
["com.company.app.core.models", "com.company.calc.algo.algorithms"],
|
|
|
|
)
|
|
|
|
assert r.ret == 1
|
|
|
|
r.stderr.fnmatch_lines("*No module named 'com.company.calc*")
|
|
|
|
|
|
|
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
|
|
|
models_py, consider_namespace_packages=True
|
|
|
|
)
|
|
|
|
assert (pkg_root, module_name) == (
|
|
|
|
tmp_path / "src/dist1",
|
|
|
|
"com.company.app.core.models",
|
|
|
|
)
|
|
|
|
|
|
|
|
# dist2/com/company will contain a normal Python package.
|
|
|
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
|
|
|
algorithms_py, consider_namespace_packages=True
|
|
|
|
)
|
|
|
|
assert (pkg_root, module_name) == (
|
|
|
|
tmp_path / "src/dist2/com/company",
|
|
|
|
"calc.algo.algorithms",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_detect_meta_path(
|
|
|
|
self,
|
|
|
|
tmp_path: Path,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
pytester: Pytester,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
resolve_pkg_root_and_module_name() considers sys.meta_path when importing namespace packages.
|
|
|
|
|
|
|
|
Regression test for #12112.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class CustomImporter(importlib.abc.MetaPathFinder):
|
|
|
|
"""
|
|
|
|
Imports the module name "com" as a namespace package.
|
|
|
|
|
|
|
|
This ensures our namespace detection considers sys.meta_path, which is important
|
|
|
|
to support all possible ways a module can be imported (for example editable installs).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def find_spec(
|
|
|
|
self, name: str, path: Any = None, target: Any = None
|
|
|
|
) -> Optional[importlib.machinery.ModuleSpec]:
|
|
|
|
if name == "com":
|
|
|
|
spec = importlib.machinery.ModuleSpec("com", loader=None)
|
|
|
|
spec.submodule_search_locations = [str(com_root_2), str(com_root_1)]
|
|
|
|
return spec
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Setup directories without configuring sys.path.
|
|
|
|
models_py, algorithms_py = self.setup_directories(
|
|
|
|
tmp_path, monkeypatch=None, pytester=pytester
|
|
|
|
)
|
|
|
|
com_root_1 = tmp_path / "src/dist1/com"
|
|
|
|
com_root_2 = tmp_path / "src/dist2/com"
|
|
|
|
|
|
|
|
# Because the namespace package is not setup correctly, we cannot resolve it as a namespace package.
|
2024-02-28 02:13:28 +08:00
|
|
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
2024-03-02 22:46:54 +08:00
|
|
|
models_py, consider_namespace_packages=True
|
2024-02-28 02:13:28 +08:00
|
|
|
)
|
|
|
|
assert (pkg_root, module_name) == (
|
|
|
|
tmp_path / "src/dist1/com/company",
|
|
|
|
"app.core.models",
|
|
|
|
)
|
2024-04-10 00:21:51 +08:00
|
|
|
|
|
|
|
# Insert our custom importer, which will recognize the "com" directory as a namespace package.
|
|
|
|
new_meta_path = [CustomImporter(), *sys.meta_path]
|
|
|
|
monkeypatch.setattr(sys, "meta_path", new_meta_path)
|
|
|
|
|
|
|
|
# Now we should be able to resolve the path as namespace package.
|
|
|
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
|
|
|
models_py, consider_namespace_packages=True
|
|
|
|
)
|
|
|
|
assert (pkg_root, module_name) == (
|
|
|
|
tmp_path / "src/dist1",
|
|
|
|
"com.company.app.core.models",
|
|
|
|
)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("insert", [True, False])
|
|
|
|
def test_full_ns_packages_without_init_files(
|
|
|
|
self, pytester: Pytester, tmp_path: Path, monkeypatch: MonkeyPatch, insert: bool
|
|
|
|
) -> None:
|
|
|
|
(tmp_path / "src/dist1/ns/b/app/bar/test").mkdir(parents=True)
|
|
|
|
(tmp_path / "src/dist1/ns/b/app/bar/m.py").touch()
|
|
|
|
|
|
|
|
if insert:
|
|
|
|
# The presence of this __init__.py is not a problem, ns.b.app is still part of the namespace package.
|
|
|
|
(tmp_path / "src/dist1/ns/b/app/__init__.py").touch()
|
|
|
|
|
|
|
|
(tmp_path / "src/dist2/ns/a/core/foo/test").mkdir(parents=True)
|
|
|
|
(tmp_path / "src/dist2/ns/a/core/foo/m.py").touch()
|
|
|
|
|
|
|
|
# Validate the namespace package by importing it in a Python subprocess.
|
|
|
|
r = validate_namespace_package(
|
|
|
|
pytester,
|
|
|
|
[tmp_path / "src/dist1", tmp_path / "src/dist2"],
|
|
|
|
["ns.b.app.bar.m", "ns.a.core.foo.m"],
|
|
|
|
)
|
|
|
|
assert r.ret == 0
|
|
|
|
monkeypatch.syspath_prepend(tmp_path / "src/dist1")
|
|
|
|
monkeypatch.syspath_prepend(tmp_path / "src/dist2")
|
|
|
|
|
|
|
|
assert resolve_pkg_root_and_module_name(
|
|
|
|
tmp_path / "src/dist1/ns/b/app/bar/m.py", consider_namespace_packages=True
|
|
|
|
) == (tmp_path / "src/dist1", "ns.b.app.bar.m")
|
|
|
|
assert resolve_pkg_root_and_module_name(
|
|
|
|
tmp_path / "src/dist2/ns/a/core/foo/m.py", consider_namespace_packages=True
|
|
|
|
) == (tmp_path / "src/dist2", "ns.a.core.foo.m")
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_importable(pytester: Pytester) -> None:
|
|
|
|
pytester.syspathinsert()
|
|
|
|
|
|
|
|
path = pytester.path / "bar/foo.py"
|
|
|
|
path.parent.mkdir()
|
|
|
|
path.touch()
|
|
|
|
assert is_importable("bar.foo", path) is True
|
|
|
|
|
|
|
|
# Ensure that the module that can be imported points to the path we expect.
|
|
|
|
path = pytester.path / "some/other/path/bar/foo.py"
|
|
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
|
|
assert is_importable("bar.foo", path) is False
|
|
|
|
|
|
|
|
# Paths containing "." cannot be imported.
|
|
|
|
path = pytester.path / "bar.x/__init__.py"
|
|
|
|
path.parent.mkdir()
|
|
|
|
path.touch()
|
|
|
|
assert is_importable("bar.x", path) is False
|
|
|
|
|
|
|
|
# Pass starting with "." denote relative imports and cannot be checked using is_importable.
|
|
|
|
path = pytester.path / ".bar.x/__init__.py"
|
|
|
|
path.parent.mkdir()
|
|
|
|
path.touch()
|
|
|
|
assert is_importable(".bar.x", path) is False
|
|
|
|
|
|
|
|
|
|
|
|
def test_compute_module_name(tmp_path: Path) -> None:
|
|
|
|
assert compute_module_name(tmp_path, tmp_path) is None
|
|
|
|
assert compute_module_name(Path(), Path()) is None
|
|
|
|
|
|
|
|
assert compute_module_name(tmp_path, tmp_path / "mod.py") == "mod"
|
|
|
|
assert compute_module_name(tmp_path, tmp_path / "src/app/bar") == "src.app.bar"
|
|
|
|
assert compute_module_name(tmp_path, tmp_path / "src/app/bar.py") == "src.app.bar"
|
|
|
|
assert (
|
|
|
|
compute_module_name(tmp_path, tmp_path / "src/app/bar/__init__.py")
|
|
|
|
== "src.app.bar"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_namespace_package(
|
|
|
|
pytester: Pytester, paths: Sequence[Path], modules: Sequence[str]
|
|
|
|
) -> RunResult:
|
|
|
|
"""
|
|
|
|
Validate that a Python namespace package is set up correctly.
|
|
|
|
|
|
|
|
In a sub interpreter, add 'paths' to sys.path and attempt to import the given modules.
|
|
|
|
|
|
|
|
In this module many tests configure a set of files as a namespace package, this function
|
|
|
|
is used as sanity check that our files are configured correctly from the point of view of Python.
|
|
|
|
"""
|
|
|
|
lines = [
|
|
|
|
"import sys",
|
|
|
|
# Configure sys.path.
|
|
|
|
*[f"sys.path.append(r{str(x)!r})" for x in paths],
|
|
|
|
# Imports.
|
|
|
|
*[f"import {x}" for x in modules],
|
|
|
|
]
|
|
|
|
return pytester.runpython_c("\n".join(lines))
|