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

Offen Anfängerfreundlich
#750 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 12 Tagen

@Omarinho12345 arbeitet bereits daran.

Seit 11.11.2026.

  • #751 von @Omarinho12345 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
85/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
backend

Rechercherichtung

Das Issue verweist auf Variable.resolve() in src/dotenv/variables.py und enthält eine minimale Reproduktion mit dotenv_values(). Lies zuerst diese Methode und die Tests zur Variablenexpansion und prüfe dann, wie ein Wert None für ${A} und ${A:-fallback} behandelt wird. Das Ziel ist erreicht, wenn für die Form mit Standardwert der Standardwert verwendet wird, während ${A} weiterhin zu einer leeren Zeichenfolge expandiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
8.9k
Forks
618
Ø Merge
12 T. 6 Min.
Gemergte PRs (30 T.)
3

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus theskumar/python-dotenv

Alle Issues in theskumar/python-dotenv

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.