Do not import duplicated modules with --importmode=importlib (#12074)
Regression brought up by #11475.
This commit is contained in:
parent
18c0e5075f
commit
03e54712dd
|
@ -0,0 +1 @@
|
||||||
|
Fixed regression where ``--importmode=importlib`` would import non-test modules more than once.
|
|
@ -539,6 +539,10 @@ def import_path(
|
||||||
except CouldNotResolvePathError:
|
except CouldNotResolvePathError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
# If the given module name is already in sys.modules, do not import it again.
|
||||||
|
with contextlib.suppress(KeyError):
|
||||||
|
return sys.modules[module_name]
|
||||||
|
|
||||||
mod = _import_module_using_spec(
|
mod = _import_module_using_spec(
|
||||||
module_name, path, pkg_root, insert_modules=False
|
module_name, path, pkg_root, insert_modules=False
|
||||||
)
|
)
|
||||||
|
|
|
@ -587,6 +587,12 @@ class TestImportLibMode:
|
||||||
assert data.value == "foo"
|
assert data.value == "foo"
|
||||||
assert data.__module__ == "_src.tests.test_dataclass"
|
assert data.__module__ == "_src.tests.test_dataclass"
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
def test_importmode_importlib_with_pickle(
|
def test_importmode_importlib_with_pickle(
|
||||||
self, tmp_path: Path, ns_param: bool
|
self, tmp_path: Path, ns_param: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -616,6 +622,12 @@ class TestImportLibMode:
|
||||||
action = round_trip()
|
action = round_trip()
|
||||||
assert action() == 42
|
assert action() == 42
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
def test_importmode_importlib_with_pickle_separate_modules(
|
def test_importmode_importlib_with_pickle_separate_modules(
|
||||||
self, tmp_path: Path, ns_param: bool
|
self, tmp_path: Path, ns_param: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -816,6 +828,14 @@ class TestImportLibMode:
|
||||||
consider_namespace_packages=ns_param,
|
consider_namespace_packages=ns_param,
|
||||||
)
|
)
|
||||||
assert len(mod.instance.INSTANCES) == 1
|
assert len(mod.instance.INSTANCES) == 1
|
||||||
|
# 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
|
||||||
|
|
||||||
def test_importlib_root_is_package(self, pytester: Pytester) -> None:
|
def test_importlib_root_is_package(self, pytester: Pytester) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -942,6 +962,15 @@ class TestImportLibMode:
|
||||||
assert mod.__name__ == "app.core"
|
assert mod.__name__ == "app.core"
|
||||||
assert mod.__file__ and Path(mod.__file__) == core_py
|
assert mod.__file__ and Path(mod.__file__) == core_py
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
# tests are not reachable from sys.path, so they are imported as a standalone modules.
|
# 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
|
# Instead of '.tests.a.test_core', we import as "_tests.a.test_core" because
|
||||||
# importlib considers module names starting with '.' to be local imports.
|
# importlib considers module names starting with '.' to be local imports.
|
||||||
|
@ -952,6 +981,16 @@ class TestImportLibMode:
|
||||||
consider_namespace_packages=ns_param,
|
consider_namespace_packages=ns_param,
|
||||||
)
|
)
|
||||||
assert mod.__name__ == "_tests.a.test_core"
|
assert mod.__name__ == "_tests.a.test_core"
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
mod = import_path(
|
mod = import_path(
|
||||||
test_path2,
|
test_path2,
|
||||||
mode="importlib",
|
mode="importlib",
|
||||||
|
@ -960,6 +999,15 @@ class TestImportLibMode:
|
||||||
)
|
)
|
||||||
assert mod.__name__ == "_tests.b.test_core"
|
assert mod.__name__ == "_tests.b.test_core"
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
def test_import_using_normal_mechanism_first_integration(
|
def test_import_using_normal_mechanism_first_integration(
|
||||||
self, monkeypatch: MonkeyPatch, pytester: Pytester, ns_param: bool
|
self, monkeypatch: MonkeyPatch, pytester: Pytester, ns_param: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -1021,6 +1069,14 @@ class TestImportLibMode:
|
||||||
assert mod.__file__ and Path(mod.__file__) == x_in_sub_folder
|
assert mod.__file__ and Path(mod.__file__) == x_in_sub_folder
|
||||||
assert mod.X == "a/b/x"
|
assert mod.X == "a/b/x"
|
||||||
|
|
||||||
|
mod2 = import_path(
|
||||||
|
x_in_sub_folder,
|
||||||
|
mode=ImportMode.importlib,
|
||||||
|
root=pytester.path,
|
||||||
|
consider_namespace_packages=ns_param,
|
||||||
|
)
|
||||||
|
assert mod is mod2
|
||||||
|
|
||||||
# Attempt to import root 'x.py'.
|
# Attempt to import root 'x.py'.
|
||||||
with pytest.raises(AssertionError, match="x at root"):
|
with pytest.raises(AssertionError, match="x at root"):
|
||||||
_ = import_path(
|
_ = import_path(
|
||||||
|
@ -1124,6 +1180,12 @@ class TestNamespacePackages:
|
||||||
assert mod.__name__ == "com.company.app.core.models"
|
assert mod.__name__ == "com.company.app.core.models"
|
||||||
assert mod.__file__ == str(models_py)
|
assert mod.__file__ == str(models_py)
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
||||||
algorithms_py, consider_namespace_packages=True
|
algorithms_py, consider_namespace_packages=True
|
||||||
)
|
)
|
||||||
|
@ -1141,6 +1203,15 @@ class TestNamespacePackages:
|
||||||
assert mod.__name__ == "com.company.calc.algo.algorithms"
|
assert mod.__name__ == "com.company.calc.algo.algorithms"
|
||||||
assert mod.__file__ == str(algorithms_py)
|
assert mod.__file__ == str(algorithms_py)
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
||||||
def test_incorrect_namespace_package(
|
def test_incorrect_namespace_package(
|
||||||
self,
|
self,
|
||||||
|
|
Loading…
Reference in New Issue