Skip to content

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

Done

Description

@Dio77799

When the variable already exists with an empty value, monkeypatch.setenv(name, value, prepend=sep) gives value + sep instead of value:

import os

def test_prepend_to_empty_variable(monkeypatch):
    monkeypatch.setenv("PYTHONPATH", "")
    monkeypatch.setenv("PYTHONPATH", "/opt/plugins", prepend=os.pathsep)
    assert os.environ["PYTHONPATH"] == "/opt/plugins"
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.

pip list
argcomplete==3.8.0
attrs==26.1.0
certifi==2026.11.20
charset-normalizer==3.5.2
codespell==2.4.3
colorama==0.4.6
coverage==7.17.4
elementpath==5.1.4
execnet==2.1.2
hypothesis==6.170.17
idna==3.20
iniconfig==2.3.1
mock==5.2.0
numpy==2.5.5
packaging==26.3
pexpect==4.9.0
pip==26.3
pluggy==1.7.0
ptyprocess==0.7.0
Pygments==2.21.0
pytest==10.0.0.dev62+g718929265
pytest-xdist==3.8.0
PyYAML==6.0.4
requests==2.34.2
ruff==0.17.5
setuptools==84.2.1
sortedcontainers==2.4.0
urllib3==2.8.0
xmlschema==4.3.2

I used Claude Code to find this and write the repro, and the commands above were run locally.

Activity

  1. added a commit that references this issue on Nov 26, 2026
    4d22064
  2. Sakibpedia commented on Nov 28, 2026

    @Sakibpedia

    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.

  3. Dio77799 commented on Nov 28, 2026

    @Dio77799
    ContributorAuthor

    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.

  4. added a commit that references this issue on Nov 28, 2026
    98b6887
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions