Fix missing argument in translation checks (#130674)

This commit is contained in:
epenet 2024-11-15 09:51:28 +01:00 committed by GitHub
parent 1e303dd706
commit 0a2535cf8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -554,6 +554,7 @@ def _validate_translation_placeholders(
full_key: str, full_key: str,
translation: str, translation: str,
description_placeholders: dict[str, str] | None, description_placeholders: dict[str, str] | None,
translation_errors: dict[str, str],
) -> str | None: ) -> str | None:
"""Raise if translation exists with missing placeholders.""" """Raise if translation exists with missing placeholders."""
tuples = list(string.Formatter().parse(translation)) tuples = list(string.Formatter().parse(translation))
@ -564,14 +565,14 @@ def _validate_translation_placeholders(
description_placeholders is None description_placeholders is None
or placeholder not in description_placeholders or placeholder not in description_placeholders
): ):
ignore_translations[full_key] = ( translation_errors[full_key] = (
f"Description not found for placeholder `{placeholder}` in {full_key}" f"Description not found for placeholder `{placeholder}` in {full_key}"
) )
async def _validate_translation( async def _validate_translation(
hass: HomeAssistant, hass: HomeAssistant,
ignore_translations: dict[str, StoreInfo], translation_errors: dict[str, str],
category: str, category: str,
component: str, component: str,
key: str, key: str,
@ -584,18 +585,18 @@ async def _validate_translation(
translations = await async_get_translations(hass, "en", category, [component]) translations = await async_get_translations(hass, "en", category, [component])
if (translation := translations.get(full_key)) is not None: if (translation := translations.get(full_key)) is not None:
_validate_translation_placeholders( _validate_translation_placeholders(
full_key, translation, description_placeholders full_key, translation, description_placeholders, translation_errors
) )
return return
if not translation_required: if not translation_required:
return return
if full_key in ignore_translations: if full_key in translation_errors:
ignore_translations[full_key] = "used" translation_errors[full_key] = "used"
return return
ignore_translations[full_key] = ( translation_errors[full_key] = (
f"Translation not found for {component}: `{category}.{key}`. " f"Translation not found for {component}: `{category}.{key}`. "
f"Please add to homeassistant/components/{component}/strings.json" f"Please add to homeassistant/components/{component}/strings.json"
) )
@ -615,7 +616,7 @@ async def _check_config_flow_result_translations(
manager: FlowManager, manager: FlowManager,
flow: FlowHandler, flow: FlowHandler,
result: FlowResult[FlowContext, str], result: FlowResult[FlowContext, str],
ignore_translations: dict[str, str], translation_errors: dict[str, str],
) -> None: ) -> None:
if result["type"] is FlowResultType.CREATE_ENTRY: if result["type"] is FlowResultType.CREATE_ENTRY:
# No need to check translations for a completed flow # No need to check translations for a completed flow
@ -649,7 +650,7 @@ async def _check_config_flow_result_translations(
for header in ("title", "description"): for header in ("title", "description"):
await _validate_translation( await _validate_translation(
flow.hass, flow.hass,
ignore_translations, translation_errors,
category, category,
integration, integration,
f"{key_prefix}step.{step_id}.{header}", f"{key_prefix}step.{step_id}.{header}",
@ -660,7 +661,7 @@ async def _check_config_flow_result_translations(
for error in errors.values(): for error in errors.values():
await _validate_translation( await _validate_translation(
flow.hass, flow.hass,
ignore_translations, translation_errors,
category, category,
integration, integration,
f"{key_prefix}error.{error}", f"{key_prefix}error.{error}",
@ -675,7 +676,7 @@ async def _check_config_flow_result_translations(
return return
await _validate_translation( await _validate_translation(
flow.hass, flow.hass,
ignore_translations, translation_errors,
category, category,
integration, integration,
f"{key_prefix}abort.{result["reason"]}", f"{key_prefix}abort.{result["reason"]}",
@ -688,12 +689,12 @@ def check_translations(ignore_translations: str | list[str]) -> Generator[None]:
"""Check that translation requirements are met. """Check that translation requirements are met.
Current checks: Current checks:
- data entry flow results (ConfigFlow/OptionsFlow) - data entry flow results (ConfigFlow/OptionsFlow/RepairFlow)
""" """
if not isinstance(ignore_translations, list): if not isinstance(ignore_translations, list):
ignore_translations = [ignore_translations] ignore_translations = [ignore_translations]
_ignore_translations = {k: "unused" for k in ignore_translations} translation_errors = {k: "unused" for k in ignore_translations}
# Keep reference to original functions # Keep reference to original functions
_original_flow_manager_async_handle_step = FlowManager._async_handle_step _original_flow_manager_async_handle_step = FlowManager._async_handle_step
@ -704,7 +705,7 @@ def check_translations(ignore_translations: str | list[str]) -> Generator[None]:
) -> FlowResult: ) -> FlowResult:
result = await _original_flow_manager_async_handle_step(self, flow, *args) result = await _original_flow_manager_async_handle_step(self, flow, *args)
await _check_config_flow_result_translations( await _check_config_flow_result_translations(
self, flow, result, _ignore_translations self, flow, result, translation_errors
) )
return result return result
@ -716,12 +717,12 @@ def check_translations(ignore_translations: str | list[str]) -> Generator[None]:
yield yield
# Run final checks # Run final checks
unused_ignore = [k for k, v in _ignore_translations.items() if v == "unused"] unused_ignore = [k for k, v in translation_errors.items() if v == "unused"]
if unused_ignore: if unused_ignore:
pytest.fail( pytest.fail(
f"Unused ignore translations: {', '.join(unused_ignore)}. " f"Unused ignore translations: {', '.join(unused_ignore)}. "
"Please remove them from the ignore_translations fixture." "Please remove them from the ignore_translations fixture."
) )
for description in _ignore_translations.values(): for description in translation_errors.values():
if description not in {"used", "unused"}: if description not in {"used", "unused"}:
pytest.fail(description) pytest.fail(description)