From ddb03069cc02d03f78c861163d6b3cb328339339 Mon Sep 17 00:00:00 2001 From: dongfangtianyu <7629022+dongfangtianyu@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:58:02 +0800 Subject: [PATCH 1/2] fix `ImportError` crash when using `--import-mode=importlib` (#13058) --- changelog/13053.bugfix.rst | 1 + src/_pytest/pathlib.py | 13 ++++++++++--- testing/test_pathlib.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 changelog/13053.bugfix.rst diff --git a/changelog/13053.bugfix.rst b/changelog/13053.bugfix.rst new file mode 100644 index 00000000000..b6744331394 --- /dev/null +++ b/changelog/13053.bugfix.rst @@ -0,0 +1 @@ +Fixed a regression in pytest 8.3.4 where, when using ``--import-mode=importlib``, a directory containing py file with the same name would cause an ``ImportError`` diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 7c368e0dcd0..f29f3c12689 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -694,9 +694,16 @@ def _import_module_using_spec( # Checking with sys.meta_path first in case one of its hooks can import this module, # such as our own assertion-rewrite hook. for meta_importer in sys.meta_path: - spec = meta_importer.find_spec( - module_name, [str(module_location), str(module_path)] - ) + module_name_of_meta = getattr(meta_importer.__class__, "__module__", "") + if module_name_of_meta == "_pytest.assertion.rewrite" and module_path.is_file(): + # import modules in subdirectories by module_path + # to ensure assertion rewrites are not missed (#12659) + find_spec_path = [str(module_location), str(module_path)] + else: + find_spec_path = [str(module_location)] + + spec = meta_importer.find_spec(module_name, find_spec_path) + if spec_matches_module_path(spec, module_path): break else: diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index a21d1edf970..5a13cd5a400 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -1498,6 +1498,34 @@ def test(): ] ) + def test_ns_multiple_levels_import_error( + self, + tmp_path: Path, + pytester: Pytester, + ) -> None: + # Trigger condition 1: ns and file with the same name + file = pytester.path / "cow/moo/moo.py" + file.parent.mkdir(parents=True) + file.write_text("data=123", encoding="utf-8") + + # Trigger condition 2: tests are located in ns + tests = pytester.path / "cow/moo/test_moo.py" + + tests.write_text( + dedent( + """ + from cow.moo.moo import data + + def test_moo(): + print(data) + """ + ), + encoding="utf-8", + ) + + result = pytester.runpytest("--import-mode=importlib") + assert result.ret == ExitCode.OK + @pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"]) def test_incorrect_namespace_package( self, From 8bedde6eb1f51d1ca7eda79133ce890055c001b7 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 12 Dec 2024 11:18:58 -0300 Subject: [PATCH 2/2] Update src/_pytest/pathlib.py --- src/_pytest/pathlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index f29f3c12689..25dc69b6349 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -696,8 +696,8 @@ def _import_module_using_spec( for meta_importer in sys.meta_path: module_name_of_meta = getattr(meta_importer.__class__, "__module__", "") if module_name_of_meta == "_pytest.assertion.rewrite" and module_path.is_file(): - # import modules in subdirectories by module_path - # to ensure assertion rewrites are not missed (#12659) + # Import modules in subdirectories by module_path + # to ensure assertion rewrites are not missed (#12659). find_spec_path = [str(module_location), str(module_path)] else: find_spec_path = [str(module_location)]