From 2fd3d3bbb7cef52d3344d53ce0650290ee0743f2 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 2 Nov 2026 12:07:40 +0100 Subject: [PATCH] Reject string flow result types in data entry flow (#189583) Co-authored-by: Claude Opus 5.5 --- homeassistant/data_entry_flow.py | 24 +++++++++++----------- tests/components/hassio/test_discovery.py | 3 ++- tests/test_data_entry_flow.py | 25 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/homeassistant/data_entry_flow.py b/homeassistant/data_entry_flow.py index 14c31ac52d65..72a701915c02 100644 --- a/homeassistant/data_entry_flow.py +++ b/homeassistant/data_entry_flow.py @@ -548,12 +548,20 @@ class FlowManager(abc.ABC, Generic[_FlowContextT, _FlowResultT, _HandlerT]): except AbortFlow as err: result = self._abort_flow_result(flow, err) + # Reject before the result is used, a plain string type would bypass the + # handling and filtering of results that compare the type by identity + if not isinstance(result["type"], FlowResultType): + if flow.flow_id in self._progress: # type: ignore[unreachable] + self._async_remove_flow_progress(flow.flow_id) + raise TypeError( + f"Flow step {step_id} of {flow.handler} returned result type " + f"{result['type']!r}, which is not a FlowResultType" + ) + if flow.flow_id not in self._progress: # The flow was removed during the step, raise UnknownFlow unless - # the result is an abort. Compares against the string value - # because this runs before the legacy-string normalization - # below, and out-of-tree flow handlers may still return raw "abort". - if result["type"] != FlowResultType.ABORT.value: + # the result is an abort. + if result["type"] is not FlowResultType.ABORT: raise UnknownFlow return result @@ -561,14 +569,6 @@ class FlowManager(abc.ABC, Generic[_FlowContextT, _FlowResultT, _HandlerT]): if result.get("preview") is not None: await self._async_setup_preview(flow) - if not isinstance(result["type"], FlowResultType): - result["type"] = FlowResultType(result["type"]) # type: ignore[unreachable] - report_usage( - "does not use FlowResultType enum for data entry flow result type", - core_behavior=ReportBehavior.LOG, - breaks_in_ha_version="2025.1", - ) - if ( result["type"] is FlowResultType.SHOW_PROGRESS # Mypy does not agree with using pop on _FlowResultT diff --git a/tests/components/hassio/test_discovery.py b/tests/components/hassio/test_discovery.py index aae53ccdf61d..5af1e2fa4fa9 100644 --- a/tests/components/hassio/test_discovery.py +++ b/tests/components/hassio/test_discovery.py @@ -24,6 +24,7 @@ from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN from homeassistant.config_entries import ConfigEntries from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers.discovery_flow import DiscoveryKey from homeassistant.helpers.service_info.hassio import HassioServiceInfo from homeassistant.setup import async_setup_component @@ -42,7 +43,7 @@ def mock_mqtt_fixture( VERSION = 1 - async_step_hassio = AsyncMock(return_value={"type": "abort"}) + async_step_hassio = AsyncMock(return_value={"type": FlowResultType.ABORT}) with mock_config_flow(MQTT_DOMAIN, MqttFlow): yield MqttFlow diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index 4bb2acddd189..9c829dca45b9 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -262,6 +262,31 @@ async def test_abort_removes_instance(manager: MockFlowManager) -> None: assert len(manager.mock_created_entries) == 0 +@pytest.mark.parametrize("result_type", ["create_entry", "form", "abort"]) +async def test_string_result_type_raises( + manager: MockFlowManager, result_type: str +) -> None: + """Test a step result with a plain string type is rejected.""" + + @manager.mock_reg_handler("test") + class TestFlow(data_entry_flow.FlowHandler): + async def async_step_init(self, user_input=None): + return { + "type": result_type, + "flow_id": self.flow_id, + "handler": self.handler, + "step_id": "init", + "title": "Test", + "data": {"password": "secret"}, + "reason": "test", + } + + with pytest.raises(TypeError, match="which is not a FlowResultType"): + await manager.async_init("test") + assert len(manager.mock_created_entries) == 0 + assert manager.async_progress(include_uninitialized=True) == [] + + @pytest.mark.parametrize( "translation_domain", [None, "homeassistant"],