Skip to content

Warn when monkeypatch.setenv(prepend=...) hits a set but empty variable, add empty= - #15433

Merged
RonnyPfannschmidt merged 3 commits into
pytest-dev:mainfrom
Dio77799:fix/setenv-prepend-empty
Nov 28, 2026
Merged

RonnyPfannschmidt merged 3 commits into
pytest-dev:mainfrom
Dio77799:fix/setenv-prepend-empty

Conversation

@Dio77799

@Dio77799 Dio77799 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

closes #15432

Description

monkeypatch.setenv(name, value, prepend=sep) gives value + sep when the variable is set but empty. What that empty entry means depends on the variable: for PATH it is the current directory, while PYTHONPATH, LD_LIBRARY_PATH and PYTEST_PLUGINS treat an empty value like an unset one. setenv can't tell them apart, so this PR keeps the current result and lets the caller choose.

  • When prepend is used and the variable is set but empty, setenv still gives value + sep and now issues a PytestWarning that explains why.
  • The new keyword only argument empty picks the result and silences the warning: empty="keep" gives value + sep, empty="skip" gives just value. Any other value raises ValueError.
  • empty has no effect when prepend is not given. Nothing changes when the variable is unset or has a value.

The setenv docstring and the monkeypatch how to now say that an empty value is not the same as unset for every variable, with PATH as the example. The changelog entry is a feature.

Two existing tests prepend to PYTHONPATH and PYTEST_PLUGINS (test_collect_pyargs_with_testpaths and test_consider_env_fails_to_import). They now pass empty="skip", so they don't warn under filterwarnings = error when those variables are set but empty, and they still prepend to a non empty inherited value.

Testing

New tests in testing/test_monkeypatch.py cover the warning and its value + sep result, both explicit empty values (no warning), an unset variable with each empty value, and the ValueError for an invalid value. That file and the two updated tests pass with -W error and PYTHONPATH set but empty (54 passed, 1 skipped). The full suite gives 4670 passed on Windows 11 with Python 3.14.4.

AI use

I made this change with Claude Code. The code and the tests were written with it, and the test runs described above were done locally while making the change. I'll answer the review myself.

Checklist

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Add text like closes #XYZW to the PR description and/or commits.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory.
  • Add yourself to AUTHORS in alphabetical order.

Copy link
Copy Markdown
Member

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

This changes behaviour that is correct for some variables, so it can't go in as a plain bugfix.

What an empty value means depends on the variable. Checked on Linux with Python 3.13:

  • PATH="" means "search the current directory" for sh, os.execvp and subprocess. Today /x: keeps that meaning; with this PR /x drops it.
  • PYTHONPATH="" and LD_LIBRARY_PATH="" are treated like unset, so /x: adds the current directory and /x is right.
  • PYTEST_PLUGINS="" loads nothing, and x, fails with Error while loading plugin "", so x is right.

setenv can't tell these apart, so neither result is right for every variable. Rather than silently switching from one to the other, I'd keep the current result and:

  • emit a warning when the variable is set but empty, since the result's meaning then depends on the variable, and
  • add a keyword so the caller can say what they want and silence the warning, for example setenv(name, value, prepend=os.pathsep, empty="keep" | "skip") (name open for discussion).

The docstring should then say that an empty value is not the same as an unset one for every variable, with PATH as the example. Do you want to rework the PR in that direction?


Generated by Claude Code

Keep the result value + prepend when the variable is set but empty, as
on main, and issue a PytestWarning instead, since an empty value means
different things for different variables. The new keyword only argument
empty="keep" | "skip" chooses the result and silences the warning.

Fixes pytest-dev#15432
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 26, 2026
@Dio77799

Dio77799 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. I reworked it: setenv with prepend now keeps the current result when the variable is set but empty, and issues a PytestWarning. The new keyword only argument empty="keep" or empty="skip" chooses the result and silences the warning; other values raise ValueError, and empty is ignored without prepend. The docstring and the monkeypatch how to say that an empty value is not the same as an unset one for every variable, with PATH as the example, and the changelog is now a feature entry.

Two existing tests set PYTHONPATH and PYTEST_PLUGINS with prepend, so they now delenv first; otherwise they would fail under filterwarnings=error when those variables exist but are empty.

Are you fine with empty as the name, or would you prefer something else?

@Dio77799 Dio77799 closed this Nov 26, 2026
@Dio77799 Dio77799 changed the title Don't add a trailing separator in monkeypatch.setenv(prepend=...) when the variable is empty Warn when monkeypatch.setenv(prepend=...) hits a set but empty variable, add empty= Nov 26, 2026
@Dio77799 Dio77799 reopened this Nov 26, 2026

Copy link
Copy Markdown
Member

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Thanks for the rework, this is the direction I meant. empty is fine as the name.

Two small things before merge:

  • In test_collect_pyargs_with_testpaths and test_consider_env_fails_to_import, delenv also drops a non-empty inherited PYTHONPATH / PYTEST_PLUGINS, which changes what those tests prepend to. Passing empty="skip" there instead keeps the old behaviour for non-empty values and uses the new keyword in our own code.
  • The PR description still describes the first version (os.environ.get, "now it leaves value"). Please update it to match the current change, since it ends up in the squash commit.

The ubuntu-py310-pluggy failure is the flaky test_pdb_unittest_postmortem pexpect timeout and unrelated; I've re-run it.


Generated by Claude Code

@Dio77799

Copy link
Copy Markdown
Contributor Author

Thanks. Both tests now pass empty="skip" instead of calling delenv (17f6436), so a non empty inherited value is still kept. I also rewrote the description to match the current change.

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Thanks, both points are addressed and CI is green. Looks good to me.


Generated by Claude Code

@RonnyPfannschmidt
RonnyPfannschmidt merged commit 98b6887 into pytest-dev:main Nov 28, 2026
36 checks passed
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

2 participants