T(...).format(n=...) crashes with ValueError when the entry's smallest plural key is greater than n
还没有人认领这个 Issue。
评估
调研方向
从 pluralize/__init__.py 中约第 168 行的 _translator 查找逻辑开始,这里会将复数键转换为整数,并根据 n 进行筛选。检查项目如何处理回退形式和格式错误的键,然后补充针对小于最小可用键的计数以及非数字键的测试。完成标准是:渲染这些翻译条目时不再抛出未处理的 ValueError,并且回退行为有测试覆盖。
由索引模型根据 Issue 内容生成。
描述
ValueError: max() iterable argument is empty when the smallest plural key in a translation is larger than n
If a translation entry only has plural forms for larger counts, rendering it with a smaller n crashes instead of falling back to a form.
Example — a partially filled cs.json (only the plural form was ever filled in, so the entry has just one key):
{"bed": {"5": "postelí"}}
>>> from pluralize import Translator
>>> T = Translator('translations')
>>> T.select('cs')
>>> print(T("bed").format(n=1))
ValueError: max() iterable argument is empty
Traceback on current master (pluralize/init.py line 168):
File "pluralize/__init__.py", line 168, in _translator
k = max(int(i) for i in translations.keys() if int(i) <= n)
ValueError: max() iterable argument is empty
The best-match lookup filters the keys with int(i) <= n, and when every key in the entry is greater than n (here: only key is 5, n is 1 — also n=0 for any entry that doesn't define a "0" form), the generator is empty and max() raises. The README's own Czech example ({"0": "no postel", "1": "postel", "2": "postele", "5": "postelí"}) shows that entries commonly skip the 0 form, so T("bed").format(n=0) on an entry like {"1": "postel", "5": "postelí"} hits the same crash.
Two smaller variants of the same line:
- a non-numeric key in an entry (files written by other i18n tools, e.g.
{"bed": {"many": "postelí"}}) raisesValueError: invalid literal for int() with base 10: 'many'from the sameint(i); - the crash happens at render time inside
str()/format(), so the error surfaces far from the file that caused it.
Suggested fix: default the lookup instead of crashing, e.g.
candidates = [int(i) for i in translations if str(int(i) if i.isdigit() else -1) == i and int(i) <= n] # or: skip non-numeric keys
if candidates:
k = max(candidates)
text = translations[str(k)].format(**kwargs)
else:
# fall back to the entry's first (smallest) form or the original text
smallest = min(translations.values()) # or keep the original text
Whatever the preferred fallback semantics, a malformed/partial translation file shouldn't take down rendering with a bare ValueError.
Verified on current master (20260519.2, pluralize/init.py:168).
- 主要语言
- Python
- 星标
- 14
- 派生
- 5
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
mloda-ai/mloda-plugin-template#206 ·
维护者通常 1 天内回复
-
integration: websocket_api
难度 2/5 1-3 小时 新手友好度 70/100
home-assistant/core#194618 · 2 条评论 ·
维护者通常 1 天内回复
-
Shorten and organize the README around reader tasks可能已有人在做 @msaad00 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 62/100
-
难度 2/5 1-3 小时 新手友好度 62/100
5hyuns/maya-workflow-kit-for-blender#568 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 80/100
letsrevel/revel-backend#1558 ·
维护者通常 1 天内回复