Skip to content

pytest 8.3.4 --import-mode importlib regression #13257

@alund

Description

@alund

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 <[email protected]>
    (cherry picked from commit 6486c3f3a858a0c8043f5c3f7c24297b82a0abe4)

    Co-authored-by: dongfangtianyu <[email protected]>

 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: collectionrelated to the collection phasetype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions