Skip to content

🧪 test: restore inherited PYTHONPATH in native env-files test - #365

Merged
gaborbernat merged 4 commits into
pytest-dev:mainfrom
sandrotosi:test/keep-pythonpath-native-env-files
Nov 27, 2026
Merged

gaborbernat merged 4 commits into
pytest-dev:mainfrom
sandrotosi:test/keep-pythonpath-native-env-files

Conversation

@sandrotosi

@sandrotosi sandrotosi commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

#290 made test_native_env_files_override_ini forward the caller's PYTHONPATH to its pytest subprocess, so packagers who reach pytest-env only through PYTHONPATH can run it. The parametrization rewrite in #308 (640b2b9) dropped that again: the test clears the environment and passes only the scenario's own PYTHONPATH, so the subprocess can't import pytest_env.plugin and all 24 cases fail:

ModuleNotFoundError: No module named 'pytest_env'. Did you mean: 'test_env'?
...
ValueError: Pytest terminal summary report not found

We hit it packaging 1.8.8 for Debian, where pybuild runs the suite against the built wheel through PYTHONPATH.

The test now prepends the caller's PYTHONPATH, if set, to the scenario's value. When PYTHONPATH is unset, as under tox, the value is an empty string, which pytester drops when it builds the subprocess's PYTHONPATH (filter(None, ...) in Pytester.popen), so the subprocess environment is unchanged; the unset scenarios still see no pythonpath_marker and the set ones still see it.

A comment on that line now records why the caller's value is kept, so a later rewrite doesn't drop it again the way #308 did.

This changes only a test, so there's no new test. I added a packaging news fragment because I can't apply the skip news label; feel free to drop it and use the label instead, as in #290.

Checked on Debian sid (Python 3.14.10, pytest 9.1.1, python-dotenv 1.2.1, pytest-mock 3.15.1), full suite:

plugin reachable through main this branch
PYTHONPATH only (built wheel unpacked) 24 failed, 171 passed 195 passed
installed, PYTHONPATH unset not run 195 passed

The 24 failures on main are all test_native_env_files_override_ini. The same change, carried as a Debian patch on 1.8.8, also passes the Debian build on Python 3.14 and 3.15. ruff 0.17.5 (format and check) and simplint 0.14.2, the versions pinned in .pre-commit-config.yaml, pass; I haven't run the other prek hooks.

pytest-dev#290 made test_native_env_files_override_ini forward the caller's
PYTHONPATH to its pytest subprocess; the rewrite in pytest-dev#308 dropped it again,
so the subprocess can't import pytest_env.plugin when the plugin is only
reachable through PYTHONPATH, and all 24 cases fail. Prepend the caller's
PYTHONPATH to the scenario's value; with PYTHONPATH unset the subprocess
environment is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sandrotosi and others added 2 commits November 26, 2026 02:03
simplint's conditional-in-test rule rejects the conditional dict
unpacking. Always set PYTHONPATH to the joined value instead: pytester's
popen rebuilds PYTHONPATH with filter(None, ...), which drops an empty
value, so the case without an inherited PYTHONPATH is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The news check needs a fragment or the skip news label, which only
maintainers can apply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@N7766 N7766 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The absolute-path fix works with an unpacked wheel and no installed plugin. One inherited-path case remains on b972310: PYTHONPATH=unpacked lets the parent import pytest_env, but pytester changes cwd before its child resolves that relative entry.

# cwd contains unpacked/, extracted from the built wheel
PYTHONPATH="$PWD/unpacked" PYTEST_PLUGINS=pytest_env.plugin python -m pytest \
  ../pytest-env-pr365/tests/test_env.py -k test_native_env_files_override_ini -q
# 24 passed
PYTHONPATH=unpacked PYTEST_PLUGINS=pytest_env.plugin python -m pytest \
  ../pytest-env-pr365/tests/test_env.py -k test_native_env_files_override_ini -q
# 24 failed: child cannot import pytest_env.plugin

Should relative entries be resolved against the original invocation directory before forwarding? This also fails on base; it is an uncovered case, not a new regression. AI-assisted reproduction on macOS/pytest 9.2.

@gaborbernat
gaborbernat merged commit 8ecd3ae into pytest-dev:main Nov 27, 2026
31 of 33 checks passed
@musicinmybrain

Copy link
Copy Markdown
Contributor

It looks like #308 broke passing through PYTHONDONTWRITEBYTECODE as well; the tests pass in Fedora packaging once I backport this PR as a patch, but I end up with an extra .pyc file after running the tests, […]/site-packages/pytest_env/__pycache__/plugin.cpython-315-pytest-9.1.1.pyc.

I tried to propose a PR similar to this one that would restore passing through PYTHONDONTWRITEBYTECODE, but there are a lot of layers and assumptions, and I had a hard time getting the tests to actually pass when I did this. I decided not to spend any more time on it.

I can deal with this for now by manually cleaning .pyc files of the form *.*-pytest-*.pyc out of the buildroot directory after running the tests, but it would be better if I could keep them from being written in the first place.

@N7766

N7766 commented Dec 1, 2026

Copy link
Copy Markdown

@musicinmybrain I reproduced the plugin .pyc on 6341877 with an unpacked wheel (macOS, Python 3.13 / pytest 9.2). Forwarding only PYTHONDONTWRITEBYTECODE=1 removes it, but fails 12 cases because their expected value is still None.

This test-only adjustment before expected_env preserves explicit scenario values and updates that expectation:

python_env = dict(python_env)
if "PYTHONDONTWRITEBYTECODE" in os.environ:
    python_env.setdefault("PYTHONDONTWRITEBYTECODE", os.environ["PYTHONDONTWRITEBYTECODE"])
expected_python = (expected_python[0], python_env.get("PYTHONDONTWRITEBYTECODE"))

Both ambient-unset and ambient-1 runs then pass all 24 cases; the latter leaves no plugin .pyc. AI-assisted local verification; Fedora/Python 3.15 and the full suite were not tested.

@pytest-dev pytest-dev locked as resolved and limited conversation to collaborators Dec 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants