Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/dotenv/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,16 @@ def __hash__(self) -> int:
def resolve(self, env: Mapping[str, Optional[str]]) -> str:
default = self.default if self.default is not None else ""
result = env.get(self.name, default)
return result if result is not None else ""
if result is None:
# A bare declaration (e.g. `BASE` with no value) parses to None.
# POSIX: under ${name:-default} that is the null case, same as
# unset, so fall back to the default when one was given.
return default
if result == "" and self.default is not None:
# POSIX: ${name:-default} falls back to the default when name
# is unset or empty (issue #715)
return default
return result


def parse_variables(value: str) -> Iterator[Atom]:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,14 @@ def test_dotenv_values_file(dotenv_path):
# Undefined
({}, "a=${b}", True, {"a": ""}),
({}, "a=${b:-d}", True, {"a": "d"}),
# Empty value with default: ${name:-default} treats empty as unset (#715)
({}, "b=\na=${b:-d}", True, {"a": "d", "b": ""}),
({"b": ""}, "a=${b:-d}", True, {"a": "d"}),
({}, "b=\na=${b}", True, {"a": "", "b": ""}),
# Bare declaration (no value, parses to None) with default:
# ${name:-default} treats it as unset/null and uses the default
({}, "b\na=${b:-d}", True, {"a": "d", "b": None}),
({}, "b\na=${b}", True, {"a": "", "b": None}),
# With quotes
({"b": "c"}, 'a="${b}"', True, {"a": "c"}),
({"b": "c"}, "a='${b}'", True, {"a": "c"}),
Expand Down
Loading