You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
monkeypatch.setenv(prepend=...) adds a trailing separator when the variable is set but empty #15432
E AssertionError: assert '/opt/plugins;' == '/opt/plugins'
E - /opt/plugins
E + /opt/plugins;
For variables that hold paths the trailing separator is not harmless. An empty entry in PYTHONPATH (or in PATH on Unix) means the current directory. With PYTHONPATH empty and prepend=os.pathsep, main leaves '/some/dir;' and a child process started from the test gets one extra entry in sys.path, the current directory. With the one line change below it is '/some/dir'.
The check in MonkeyPatch.setenv is if prepend and name in os.environ: (src/_pytest/monkeypatch.py:350). It asks whether the variable exists, not whether it has a value. if prepend and os.environ.get(name): fixes it. I did not strip the separator after joining, because that would also change values that end with the separator on purpose.
I can send a PR with a test.
Found on main at 7189292 (pytest 10.0.0.dev62+g718929265), Python 3.14.4, Windows 11. The logic does not depend on the platform.
Hi @Dio77799 — I'd like to take this one. Your diagnosis is precise (if prepend and name in os.environ: should check the value, not just presence) and there's no open PR for it six days on, so I'll open one with a regression test that covers the empty-variable case and keeps the deliberate trailing-separator case working. Please shout if you're already working on it and I'll stand down.
Hi @Dio77799 — I'd like to take this one. Your diagnosis is precise (if prepend and name in os.environ: should check the value, not just presence) and there's no open PR for it six days on, so I'll open one with a regression test that covers the empty-variable case and keeps the deliberate trailing-separator case working. Please shout if you're already working on it and I'll stand down.
Hi @Sakibpedia, thanks for the interest. This one is already covered: I opened #15433 for it and it closes this issue. Ronny reviewed it and asked for a different approach (an empty value now keeps the current result, warns, and can be chosen with a new empty= keyword). I pushed the two follow ups he asked for, so it is waiting on a final look. If you want to help, trying that branch on your setup would be welcome.
Activity
Warn on setenv prepend to an empty variable and add the empty keyword
Sakibpedia commentedon Nov 28, 2026
Hi @Dio77799 — I'd like to take this one. Your diagnosis is precise (
if prepend and name in os.environ:should check the value, not just presence) and there's no open PR for it six days on, so I'll open one with a regression test that covers the empty-variable case and keeps the deliberate trailing-separator case working. Please shout if you're already working on it and I'll stand down.Dio77799 commentedon Nov 28, 2026
Hi @Sakibpedia, thanks for the interest. This one is already covered: I opened #15433 for it and it closes this issue. Ronny reviewed it and asked for a different approach (an empty value now keeps the current result, warns, and can be chosen with a new
empty=keyword). I pushed the two follow ups he asked for, so it is waiting on a final look. If you want to help, trying that branch on your setup would be welcome.Warn when `monkeypatch.setenv(prepend=...)` hits a set but empty vari…