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

未關閉 適合新手
#750 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 12 天內回覆

@Omarinho12345 已經在處理了。

開始於 2026年11月11日。

  • #751 來自 @Omarinho12345 —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
85/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
backend

研究方向

Issue 指出 src/dotenv/variables.py 中的 Variable.resolve(),並提供使用 dotenv_values() 的最小重現範例。先閱讀該方法和變數展開測試,接著檢查 ${A} 和 ${A:-fallback} 如何處理 None 值。完成標準是:帶有預設值的形式使用預設值,而一般的 ${A} 仍展開為空字串。

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
Python
星號
8.9k
分支
618
平均合併
12 天 6 分鐘
30 天內合併 PR
3

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

theskumar/python-dotenv 的其他 Issue

查看 theskumar/python-dotenv 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。