Starting in 8.3.4, running pytest using --import-mode importlib with the following directory structure no longer works.
<root>
+ mypkg
- pyproject.toml
+ mypkg
- __init__.py
+ tests
- test_version.py
test_version.py attempts to import from mypkg and fails:
mypkg/tests/version_test.py:1: in <module>
from mypkg import version
E ImportError: cannot import name 'version' from 'mypkg' (unknown location)
pytest.ini contains:
[pytest]
addopts = --import-mode importlib
testpaths =
mypkg/tests
git bisect says:
± git bisect bad
3d3ec5724c6f76bc07d0631ec8061f26f9ecac4c is the first bad commit
commit 3d3ec5724c6f76bc07d0631ec8061f26f9ecac4c
Author: patchback[bot] <45432694+patchback[bot]@users.noreply.github.com>
Date: Thu Sep 26 01:26:01 2024 +0000
Fix KeyError with importlib mode (directories with same name) (#12752) (#12843)
Directories inside a namespace package with the same name as the namespace package would cause a `KeyError` with `--import-mode=importlib`.
Fixes #12592
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
(cherry picked from commit 6486c3f3a858a0c8043f5c3f7c24297b82a0abe4)
Co-authored-by: dongfangtianyu <7629022+dongfangtianyu@users.noreply.github.com>
changelog/12592.bugfix.rst | 1 +
src/_pytest/pathlib.py | 131 +++++++++++++++++++++++++++++++++------------
testing/test_pathlib.py | 74 ++++++++++++++++++++++++-
3 files changed, 171 insertions(+), 35 deletions(-)
create mode 100644 changelog/12592.bugfix.rst
I'm attaching a minimal project that should reproduce the problem.
reproduce.zip
Starting in 8.3.4, running
pytestusing--import-mode importlibwith the following directory structure no longer works.test_version.pyattempts to import frommypkgand fails:pytest.inicontains:git bisectsays:I'm attaching a minimal project that should reproduce the problem.
reproduce.zip