Skip to content

Fix monkeypatch.setenv prepend with an empty environment value - #15493

Closed
Sakibpedia wants to merge 1 commit into
pytest-dev:mainfrom
Sakibpedia:fix/monkeypatch-setenv-prepend-empty
Closed

Sakibpedia wants to merge 1 commit into
pytest-dev:mainfrom
Sakibpedia:fix/monkeypatch-setenv-prepend-empty

Conversation

@Sakibpedia

Copy link
Copy Markdown

Fixes #15432

monkeypatch.setenv(name, value, prepend=sep) checked name in os.environ, so a variable that was already set to an empty string got value + sep + "" — a trailing separator. For path-like variables that silently adds the current directory as an extra entry.

Changed the condition to os.environ.get(name) so only a non-empty existing value is joined. A value that intentionally ends with the separator is left untouched.

Added a regression test next to the existing prepend test and the 15432.bugfix.rst changelog fragment.

Verification

Test against the unmodified source:

E   AssertionError: assert '/opt/plugins;' == '/opt/plugins'

After the fix:

python -m pytest -p no:asyncio testing/test_monkeypatch.py -q
47 passed in 4.47s

ruff check and ruff format --check both pass on the changed files.

AI assistance

AI assistance (OpenCode) was used to write this change. The change is a one-line condition plus a regression test; I reviewed every line, confirmed the test fails before the fix and passes after, and ran the full test_monkeypatch.py module plus the repository's Ruff checks.

Use os.environ.get(name) so an existing but empty value is not joined with the prepend separator.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 28, 2026
@Sakibpedia

Copy link
Copy Markdown
Author

Closing as superseded: upstream main now includes #15433 ("Warn when monkeypatch.setenv(prepend=...) hits a set but empty variable, add empty="), which addresses this case more thoroughly than this patch. The conflicts when updating the branch are from that change. Thanks for reviewing — no action needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

monkeypatch.setenv(prepend=...) adds a trailing separator when the variable is set but empty

1 participant