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

Aperta Adatta ai principianti
#750 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 12 giorni

@Omarinho12345 ci sta già lavorando.

Dal 11/11/2026.

  • #751 di @Omarinho12345 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

L’issue indica Variable.resolve() in src/dotenv/variables.py e fornisce una riproduzione minima con dotenv_values(). Leggi prima quel metodo e i test di espansione delle variabili, poi verifica come viene gestito un valore None per ${A} e ${A:-fallback}. Il risultato atteso è che venga usato il valore predefinito per la forma che lo specifica, mentre ${A} continui a espandersi in una stringa vuota.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
8.9k
Fork
618
Merge medio
12g 6m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di theskumar/python-dotenv

Tutte le issue di theskumar/python-dotenv

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.