python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

Variable declared without a value suppresses ${VAR:-default} expansion

Open Omarinho12345 opened this issue 3 weeks ago • 0 comments

Environment

  • python-dotenv 1.2.4 (main, commit 4fbc6ff)
  • Python 3.14.0 on Windows 11; the code involved is platform independent

Minimal repro

import io
from dotenv import dotenv_values

print(dotenv_values(stream=io.StringIO("A\nB=${A:-fallback}")))

Actual

OrderedDict({'A': None, 'B': ''})

Expected

OrderedDict({'A': None, 'B': 'fallback'})

${A} should still expand to ""; only the defaulted form is affected.

Why this is wrong

The README's "Variable expansion" section lists the resolution order for dotenv_values() as: value of the variable in the .env file, value in the environment, default value, then empty string. A key written without a value (A) is documented earlier as producing None, and load_dotenv is said to "simply ignore such variables" — that is, the variable is unset, not set to the empty string. Since the default is only skipped when the variable has a value, ${A:-fallback} should produce fallback here.

This also contradicts the explicit distinction the README draws between FOO and FOO=: A and A= currently expand identically ("") even though only A= is the empty string.

The cause is in Variable.resolve() (src/dotenv/variables.py): a name that is present in the mapping with a None value is converted to "", so the supplied default is never used.

Omarinho12345 avatar Nov 11 '26 17:11 Omarinho12345