Repository navigation
🐛 fix(dotenv): expand references after skip-if-set - #340
Merged
gaborbernat merged 1 commit intoNov 17, 2026
Merged
Conversation
python-dotenv expanded ${VAR} while reading a file, letting the file's own
earlier assignment win over the environment, and only then did
env_files_skip_if_set decide which keys to keep. A variable the rule kept
and a second one built from it therefore disagreed: DB_HOST stayed at the
exported host while DATABASE_URL pointed at the host from the file.
With the option on, each value is now expanded when it is applied, against
the environment as it stands. Without it the file is read as before.
Reported by Anthropic's OSS Scanner: ANT-2026-5JX2GCSN.
Fixes pytest-dev#327
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_apply_env_fileread each.envfile withdotenv_valuesand appliedenv_files_skip_if_setafterwards, key by key (old lines). The values were already expanded by then, with the file's own assignments taking priority. WithDB_HOST=ci-db.internalexported and a file holdingDB_HOST=localhostandDATABASE_URL=postgres://${DB_HOST}/app, the tests saw the exportedDB_HOSTand aDATABASE_URLthat pointed atlocalhost. 🐛 After this PR the reference expands to the value that was kept. Fixes #327.Expansion ran before the skip rule
dotenv_valuesinterpolates withoverride=True, and in that moderesolve_variableslets a value assigned earlier in the file win overos.environ. pytest-env then skippedDB_HOSTand kept the already expandedDATABASE_URL.With the option on,
_apply_env_filenow reads the raw values and expands each one when it applies it, againstos.environas it stands, using python-dotenv's ownparse_variables. A skipped key is not expanded. With the option off, the file is read withdotenv_valuesas before.Behavior changes
Under
env_files_skip_if_set = true, a${VAR}reference sees the value the variable has in the session: the kept one when the file's assignment was skipped, the file's when it was applied.${VAR:-default}works as before. One edge differs under the option: when a file assigns the same key twice, a reference between the two assignments now expands to the last assignment, because python-dotenv returns one value per key. Files read without the option are not affected.dotenv.variablesis not part of python-dotenv's documented API, so a future release of it could require a change here.Reported by Anthropic's OSS Scanner: ANT-2026-5JX2GCSN.