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 摘要。