Substitute ${name:-default} when the variable is set but empty (POSIX) - #684
Open
ckarnell wants to merge 1 commit into
Open
Substitute ${name:-default} when the variable is set but empty (POSIX)#684ckarnell wants to merge 1 commit into
ckarnell wants to merge 1 commit into
Conversation
The README documents POSIX variable expansion, where ${name:-word} substitutes when name is unset OR null. resolve() used env.get(name, default), which falls back only when the key is absent, so a variable that exists and is empty returned empty. Resolve the name first and fall back when the result is empty. ${name} with no default and ${name:-} on an empty value are unchanged.
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.
The README says interpolation follows POSIX variable expansion.
${name:-word}in POSIX substituteswordwhennameis unset or null, and this only handles unset:env.getfalls back only when the key is absent, so a variable that exists and is empty returns empty.The unset case and the set-to-a-value case both already match, so this is the one branch that doesn't.
The change resolves the name first and falls back to the default when the result is empty, which leaves
${b}with no default alone and keeps${b:-}empty.Four cases added to the
test_dotenv_values_string_iotable. Two fail on main, the empty-with-a-default ones. The other two pin${b}and${b:-}on an empty value, which behave the same before and after. Tests go from 226 to 230 passing, with the same 12 pre-existingtest_cli.pyfailures either way. Those areFileNotFoundErroron subprocess spawning in my environment, not this path.This changes behaviour. Anyone who wrote
${VAR:-default}and relied on an emptyVARstaying empty gets the default instead now. That's what POSIX and the README both describe, but it's a change, so it's your call whether it belongs in a minor release or wants a note.