pytest-dev / pytest-dev/pytest

`--doctest-modules` option causes namespace packages' subpackages to be added to `sys.path`

Open
#12,287 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

I created a simple namespace package with the following structure:

.
├── pyproject.toml
└── src
    ├── my_ns
    │   └── my_pkg
    │       ├── README.md
    │       └── __init__.py
    └── tests
        ├── __init__.py
        └── test_my_pkg.py

In the test_my_pkg.py file, I print the content of sys.path.

# test_my_pkg.py
from my_ns import my_pkg

import sys

def test_package_version():
    print("\n\nsys path = ", str(sys.path))
    assert my_pkg.__version__ == "0.0.1"

I noticed that when I execute pytest with the option --doctest-modules, src/my_ns is added to my sys.path.
While, without the option, src/my_ns it is not added (only src/, which makes sense).

I wanted to check with you if this is expected behaviour. In particular I have an external dependency called my_pkg. Which caused a weird conflict during pytest execution.

Env characteristics

pip list
Package        Version
-------------- -------
exceptiongroup 1.2.1
iniconfig      2.0.0
packaging      24.0
pip            23.0.1
pluggy         1.5.0
pytest         8.2.0
setuptools     65.5.0
tomli          2.0.1
pytest and operating system versions
pytest 8.2.0
macOS 14.4.1
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

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 with the minimal namespace-package tree and run pytest with and without --doctest-modules, comparing sys.path in test_my_pkg.py. Trace the collection behavior for doctest modules and determine whether adding src/my_ns is intentional; done means the behavior is documented or corrected so the external my_pkg conflict is addressed.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.