From 2aec104a8fe4b18382cc1f87434f4fc638d43d64 Mon Sep 17 00:00:00 2001 From: bug-origin <29540617+bug-origin@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:52:42 +0900 Subject: [PATCH] Reject non-boolean TOML defaults for pybabel flags --- babel/messages/frontend.py | 5 +++++ tests/messages/test_toml_config.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index d32104f88..10b463f6b 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -1497,6 +1497,11 @@ def _configure_command(self, cmdname: str, argv: list[str]) -> CommandMixin: if name == as_args: parser.usage += f"<{name}>" elif name in cmdclass.boolean_options: + if normalized_name in toml_config and not isinstance(default, bool): + parser.error( + f"[tool.babel.{cmdinst.toml_config_table}] {normalized_name} " + "must be a TOML boolean (true or false)", + ) parser.add_option(*strs, action="store_true", default=default, help=help) elif name in cmdclass.multiple_value_options: default_list = [default] if isinstance(default, str) else default diff --git a/tests/messages/test_toml_config.py b/tests/messages/test_toml_config.py index f6e139c8c..f4ec19a34 100644 --- a/tests/messages/test_toml_config.py +++ b/tests/messages/test_toml_config.py @@ -178,3 +178,33 @@ def test_cli_pyproject_toml_list_values_not_split(in_tmp_path): cmdinst = cli._configure_command("extract", []) assert cmdinst.add_comments == ["NOTE TO TRANSLATORS:"] assert cmdinst.input_paths == ["a b.py"] + + +@pytest.mark.parametrize("value", ['"false"', "0"]) +def test_update_rejects_non_boolean_ignore_obsolete(in_tmp_path, capsys, value): + (in_tmp_path / "pyproject.toml").write_text(f"[tool.babel.update_catalog]\nignore_obsolete = {value}\n") + po_file = in_tmp_path / "messages.po" + po_file.write_bytes(b'msgid "old"\nmsgstr "Old translation"\n') + original = po_file.read_bytes() + (in_tmp_path / "messages.pot").write_bytes(b'msgid "new"\nmsgstr ""\n') + + with pytest.raises(SystemExit) as exc_info: + frontend.CommandLineInterface().run( + ["pybabel", "update", "-l", "en", "-i", "messages.pot", "-o", "messages.po"], + ) + + assert exc_info.value.code == 2 + assert "ignore_obsolete must be a TOML boolean" in capsys.readouterr().err + assert po_file.read_bytes() == original + + +@pytest.mark.parametrize( + ("value", "flags", "expected"), + [("true", [], True), ("false", [], False), ("false", ["--ignore-obsolete"], True)], +) +def test_update_boolean_ignore_obsolete(in_tmp_path, value, flags, expected): + (in_tmp_path / "pyproject.toml").write_text(f"[tool.babel.update_catalog]\nignore_obsolete = {value}\n") + cli = frontend.CommandLineInterface() + cli.log = frontend.log + command = cli._configure_command("update", ["-i", "messages.pot", "-d", "locale", *flags]) + assert command.ignore_obsolete is expected