T(...).format(n=...) crashes with ValueError when the entry's smallest plural key is greater than n

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

还没有人认领这个 Issue。

评估

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

调研方向

从 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í"}}) raises ValueError: invalid literal for int() with base 10: 'many' from the same int(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,通用步骤见我们的新手贡献指南。

从这里开始

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

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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