From 44de1e4d7e12b52cb82d175301ed7ea0c13800e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=91=D1=8B=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B2?= Date: Wed, 11 Nov 2026 18:51:12 +0500 Subject: [PATCH] Fix validation of repeated named format conversions Keep all source conversion types for each named placeholder instead of discarding all but its last occurrence. Match each translated conversion against those source types using the existing compatibility rules. Cover identical and reordered translations, omitted occurrences, incompatible conversions, plural catalogs, and compilation with a GNUTranslations round-trip. --- babel/messages/checkers.py | 12 +++++--- tests/messages/frontend/test_cli.py | 27 ++++++++++++++++++ tests/messages/test_checkers.py | 44 +++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/babel/messages/checkers.py b/babel/messages/checkers.py index cf0fbcd39..6f58e4410 100644 --- a/babel/messages/checkers.py +++ b/babel/messages/checkers.py @@ -153,16 +153,20 @@ def _check_positional(results: list[tuple[str, str]]) -> bool: ) # otherwise the second string must not have names the first one - # doesn't have and the types of those included must be compatible + # doesn't have, and each type must match a type used by the same name + # in the first string. else: - type_map = dict(a) + type_map: dict[str, set[str]] = {} + for name, typechar in a: + type_map.setdefault(name, set()).add(typechar) for name, typechar in b: if name not in type_map: raise TranslationError(f'unknown named placeholder {name!r}') - elif not _compatible(typechar, type_map[name]): + elif not any(_compatible(typechar, source_type) for source_type in type_map[name]): + source_types = ', '.join(repr(source_type) for source_type in sorted(type_map[name])) raise TranslationError( f'incompatible format for placeholder {name!r}: ' - f'{typechar!r} and {type_map[name]!r} are not compatible', + f'{typechar!r} and {source_types} are not compatible', ) diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index a2b85bfba..80e5d5fef 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -18,6 +18,7 @@ import sys import time from datetime import datetime, timedelta +from gettext import GNUTranslations from io import StringIO import pytest @@ -419,6 +420,32 @@ def test_compile_catalog(cli): assert sys.stderr.getvalue() == f'catalog {po_file} is marked as fuzzy, skipping\n' +@pytest.mark.parametrize( + ('msgid', 'msgstr', 'value', 'expected'), + [ + ('Value: %(value)s (%(value)d)', 'Wert: %(value)d (%(value)s)', 42, 'Wert: 42 (42)'), + ('Code: %(value)d (0x%(value)x)', 'Code: %(value)d (0x%(value)x)', 42, 'Code: 42 (0x2a)'), + ('Name: %(value)s (%(value)r)', 'Name: %(value)r (%(value)s)', 'word', "Name: 'word' (word)"), + ], +) +def test_compile_catalog_repeated_named_types(cli, tmp_path, msgid, msgstr, value, expected): + catalog = Catalog(locale='de', fuzzy=False) + catalog.add(msgid, msgstr) + po_file = tmp_path / 'messages.po' + mo_file = tmp_path / 'messages.mo' + with po_file.open('wb') as fileobj: + write_po(fileobj, catalog) + + assert cli.run([ + 'pybabel', 'compile', '--locale', 'de', + '--input-file', str(po_file), '--output-file', str(mo_file), + ]) == 0 + + with mo_file.open('rb') as fileobj: + translations = GNUTranslations(fileobj) + assert translations.gettext(msgid) % {'value': value} == expected + + def test_compile_fuzzy_catalog(cli): po_file = get_po_file_path('de_DE') mo_file = po_file.replace('.po', '.mo') diff --git a/tests/messages/test_checkers.py b/tests/messages/test_checkers.py index 01194e467..9d6caba9e 100644 --- a/tests/messages/test_checkers.py +++ b/tests/messages/test_checkers.py @@ -370,6 +370,50 @@ def test_python_format_valid(msgid, msgstr): python_format(None, msg) +@pytest.mark.parametrize( + ('msgid', 'msgstr', 'value'), + [ + ('%(value)s %(value)d', '%(value)s %(value)d', 42), + ('%(value)d %(value)s', '%(value)s %(value)d', 42), + ('%(value)s %(value)d', '%(value)s', 42), + ('%(value)r %(value)s', '%(value)s %(value)r', 'word'), + ('%(value)x %(value)s', '%(value)s %(value)X', 42), + ('%(value)d %(value)x', '%(value)X %(value)i', 42), + ], +) +def test_python_format_repeated_named_types(msgid, msgstr, value): + arguments = {'value': value} + assert isinstance(msgid % arguments, str) + assert isinstance(msgstr % arguments, str) + python_format(None, Message(msgid, msgstr)) + + +@pytest.mark.parametrize( + ('msgid', 'msgstr', 'value'), + [ + ('%(value)s %(value)f', '%(value)x', 1.5), + ('%(value)s %(value)r', '%(value)d', 'word'), + ('%(value)s %(value)s', '%(value)d', 'word'), + ], +) +def test_python_format_repeated_named_types_invalid(msgid, msgstr, value): + arguments = {'value': value} + assert isinstance(msgid % arguments, str) + with pytest.raises(TypeError): + msgstr % arguments + with pytest.raises(TranslationError, match="incompatible format for placeholder 'value'"): + python_format(None, Message(msgid, msgstr)) + + +def test_python_format_repeated_named_types_plural_catalog(): + catalog = Catalog(locale='ru') + catalog.add( + ('%(value)s: %(value)d item', '%(value)s: %(value)d items'), + ('%(value)d (%(value)s)', '%(value)s (%(value)i)', '%(value)d'), + ) + assert list(catalog.check()) == [] + + @pytest.mark.parametrize( ('msgid', 'msgstr', 'arguments'), [