pytest-dev / pytest-dev/pytest

pytest 8.3.4 --import-mode importlib regression

Open
#13,257 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection type: regression
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with the attached minimal project using --import-mode importlib, then read the changes from commit 3d3ec5724c6f76bc07d0631ec8061f26f9ecac4c in src/_pytest/pathlib.py. Review the related tests in testing/test_pathlib.py and add coverage for the reported directory layout. Done means the import succeeds and the pathlib tests pass without regressing the KeyError fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.