Skip to content

Fix ${name:-default} ignoring set-but-empty variables - #736

Closed
mayur-dhavan wants to merge 2 commits into
theskumar:mainfrom
mayur-dhavan:fix-posix-default-on-empty
Closed

mayur-dhavan wants to merge 2 commits into
theskumar:mainfrom
mayur-dhavan:fix-posix-default-on-empty

Conversation

@mayur-dhavan

Copy link
Copy Markdown

Fixes #715.

Problem

POSIX ${name:-default} applies the default when name is unset or empty. python-dotenv only applied it when the variable was unset, so an explicitly empty value leaked through:

dotenv.dotenv_values(stream=io.StringIO("b=\na=${b:-d}"))
# before: {'b': '', 'a': ''}
# after:  {'b': '', 'a': 'd'}

b unset already produced d, and ${b} without a default correctly stays empty - only the empty-with-default case was wrong.

Fix

In Variable.resolve (src/dotenv/variables.py), when a default was specified and the resolved value is the empty string, return the default. The no-default path (${name}) is unchanged, and a set non-empty value still wins.

Tests

Three new parametrized cases in test_dotenv_values_string_io: empty value from the environment, empty value from the file, and empty value without a default (unchanged behavior). Verified locally on Python 3.13: tests/test_main.py + tests/test_variables.py pass (165 passed). The pre-existing test_cli.py PermissionErrors are a sandbox restriction also present on unmodified master.

AI disclosure: this contribution was prepared with AI assistance (an automated coding agent acting on behalf of the author); the change and tests were validated locally against the project's test suite as described above.

Copilot AI balanced review requested due to automatic review settings October 25, 2026 18:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@mayur-dhavan

Copy link
Copy Markdown
Author

Closing this as a duplicate: #716 (Oct 1) and #720 (Oct 4) both fix #715 with the same POSIX :- semantics (default applies when the variable is unset OR empty) and equivalent tests. I missed both when checking for existing work - a third PR on the same fix adds nothing. Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An empty value ignores ${name:-default}

2 participants