Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

An empty list or mapping is expanded as a defined value: `{x,v}` with `v=[]` gives `1,`, `{#v}` gives `#`, `{;v}` gives `;v=`

未关闭 适合新手
#164 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

阅读uritemplate/variable.py,重点查看_string_expansion和_semi_path_expansion,并将它们对空列表和映射的处理方式与_query_expansion和_label_path_expansion进行比较。使用 issue 中的 Python 命令重现报告的案例,然后运行项目和 RFC uritemplate-test测试套件。受影响的扩展将空集合视为未定义,并且列出的预期输出均通过后,即算完成。

由索引模型根据 Issue 内容生成。

描述

Versions tested: uritemplate 4.2.0 from PyPI (the newest release on 2026-10-23) and the main branch at
8e46228e5a1ad3f688b21c6b3917d15dd0f7c581 (its head on 2026-10-23); CPython 3.10 and 3.11 on Linux.

Reproduce (one command):

python3 -c 'from uritemplate import expand; print(repr(expand("{x,v}", {"x": "1", "v": []})))'

An empty list is an ordinary value for a variable that has none to give, and RFC 6570 covers it.

Expected: '1'. RFC 6570 section 2.3: "A variable defined as a list value is considered undefined if the list contains zero members. A
variable defined as an associative array of (name, value) pairs is considered undefined if the array contains zero members or if all member
names in the array are associated with undefined values." Section 3.2.1: "A variable that is undefined (Section 2.3) has no value and is ignored by the expansion process. If all of the
variables in an expression are undefined, then the expression's expansion is the empty string." The algorithm in Appendix A says the same:
"If the varname is unknown or corresponds to a variable with an undefined value (Section 2.3), then skip to the next varspec". The separator
is appended only between defined variables.

Observed (the same on 4.2.0 and on main):

'1,'

More cases, all with x="1" (tool output / what the RFC text gives):

template v observed expected
{+x,v} [] 1, 1
{#x,v} [] #1, #1
{#v} [] or {} # (empty)
{;x,v} [] ;x=1;v= ;x=1
{;v} {} ;v= (empty)
{;v*} {} ; (empty)

Controls, correct today: {/x,v} -> /1, {.x,v} -> .1, {?x,v} -> ?x=1, {&x,v} -> &x=1, and {x,w} with w not given -> 1.

Where

In uritemplate/variable.py (same on main and in 4.2.0), _query_expansion returns None for an empty list or mapping (if not value: return None, in both the list and the mapping branch) and _label_path_expansion does the same (len(value) == 0), which is why /, ., ? and & are correct. _string_expansion (used by the simple, + and # operators) has no such check, and _semi_path_expansion has it only in its list-explode branch ({;v*} with [] correctly gives ''), so in the other cases the empty collection is expanded as a defined, empty value. Adding the same early return to those two functions made every case above agree with the RFC text and left the project's own test suite and the RFC's uritemplate-test suite green on this machine; that is only a local check, not a proposed patch.

Found by comparing the library with a small model of the RFC's expansion algorithm on generated templates and values, then confirmed against an independent implementation (uri-template 1.3.0 on PyPI), which gives '1', '' and ';v' for the three cases.

主要语言
Python
星标
246
派生
41
平均合并
8 小时 20 分钟
30 天内合并 PR
3

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-hyper/uritemplate 的其他 Issue

查看 python-hyper/uritemplate 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。