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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- `${name:-default}` now uses the default when `name` is empty. `b=` followed by `a=${b:-d}` yields `d` instead of an empty string. ([#715])
- Fix a package build deprecation warning caused by a non-string `license` value in `pyproject.toml` by [@kurtmckee] in [#648]
- `set_key`, `unset_key` and the `dotenv set`/`unset` commands now name the `.env` path instead of an internal temporary file when its directory is missing or not writable, and the CLI prints a short error and exits with code 2 instead of a traceback by [@jamalkamaladdin] in [#711]
- `set_key` and `unset_key` no longer leave a `.tmp_*` file behind on Windows when writing a read-only `.env` fails, and the error raised is the one from the failed write rather than from cleaning up the temporary file by [@MohammedAlkindi] in [#686]
Expand Down
11 changes: 8 additions & 3 deletions src/dotenv/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ def __hash__(self) -> int:
return hash((self.__class__, self.name, self.default))

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 ""
# ${name:-default} uses the default when name is missing or empty.
# ${name} has no default, so those cases stay empty.
result = env.get(self.name)
if result:
return result
if self.default is not None:
return self.default
return result or ""


def parse_variables(value: str) -> Iterator[Atom]:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ def test_dotenv_values_file(dotenv_path):
# Undefined
({}, "a=${b}", True, {"a": ""}),
({}, "a=${b:-d}", True, {"a": "d"}),
# Empty is unset for the :- form
({"b": ""}, "a=${b:-d}", True, {"a": "d"}),
({}, "b=\na=${b:-d}", True, {"a": "d", "b": ""}),
# With quotes
({"b": "c"}, 'a="${b}"', True, {"a": "c"}),
({"b": "c"}, "a='${b}'", True, {"a": "c"}),
Expand Down
7 changes: 7 additions & 0 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ def test_parse_variables(value, expected):
result = parse_variables(value)

assert list(result) == expected


def test_empty_value_uses_default():
assert Variable(name="b", default="d").resolve({"b": ""}) == "d"
assert Variable(name="b", default="d").resolve({}) == "d"
assert Variable(name="b", default="d").resolve({"b": "c"}) == "c"
assert Variable(name="b", default=None).resolve({"b": ""}) == ""
Loading