Repository navigation
Warn when monkeypatch.setenv(prepend=...) hits a set but empty variable, add empty= - #15433
Conversation
|
🤖 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:
The docstring should then say that an empty value is not the same as an unset one for every variable, with 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
|
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 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 |
monkeypatch.setenv(prepend=...) when the variable is emptymonkeypatch.setenv(prepend=...) hits a set but empty variable, add empty=
|
🤖 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. Two small things before merge:
The Generated by Claude Code |
|
Thanks. Both tests now pass |
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 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
closes #15432
Description
monkeypatch.setenv(name, value, prepend=sep)givesvalue + sepwhen the variable is set but empty. What that empty entry means depends on the variable: forPATHit is the current directory, whilePYTHONPATH,LD_LIBRARY_PATHandPYTEST_PLUGINStreat an empty value like an unset one.setenvcan't tell them apart, so this PR keeps the current result and lets the caller choose.prependis used and the variable is set but empty,setenvstill givesvalue + sepand now issues aPytestWarningthat explains why.emptypicks the result and silences the warning:empty="keep"givesvalue + sep,empty="skip"gives justvalue. Any other value raisesValueError.emptyhas no effect whenprependis not given. Nothing changes when the variable is unset or has a value.The
setenvdocstring and the monkeypatch how to now say that an empty value is not the same as unset for every variable, withPATHas the example. The changelog entry is a feature.Two existing tests prepend to
PYTHONPATHandPYTEST_PLUGINS(test_collect_pyargs_with_testpathsandtest_consider_env_fails_to_import). They now passempty="skip", so they don't warn underfilterwarnings = errorwhen those variables are set but empty, and they still prepend to a non empty inherited value.Testing
New tests in
testing/test_monkeypatch.pycover the warning and itsvalue + sepresult, both explicitemptyvalues (no warning), an unset variable with eachemptyvalue, and theValueErrorfor an invalid value. That file and the two updated tests pass with-W errorandPYTHONPATHset 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
closes #XYZWto the PR description and/or commits.Co-authored-bycommit trailers.changelogdirectory.AUTHORSin alphabetical order.