T(...).format(n=...) crashes with ValueError when the entry's smallest plural key is greater than n
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 74/100
Direzione di ricerca
Inizia in pluralize/__init__.py, nella ricerca di _translator intorno alla riga 168, dove le chiavi plurali vengono convertite in interi e filtrate in base a n. Verifica come il progetto gestisce le forme di ripiego e le chiavi malformate, quindi aggiungi test per conteggi inferiori alla chiave disponibile più piccola e per chiavi non numeriche. Il lavoro è completato quando il rendering non solleva più un ValueError non gestito per queste voci di traduzione e il comportamento di ripiego è coperto dai test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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).
- Lingua principale
- Python
- Stelle
- 14
- Fork
- 5
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
Library sweep logs an ERROR at start for the valkey instrumentor when valkey-py is not installedAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
bug good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
JoahanMorales/upiixia-agentic-kit#99 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
NatLabRockies/openstudio-mcp#286 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100