Remove config flow specifics from FlowResult (#111932)

* Remove config flow specifics from FlowResult

* Improve docstring

* Update pylint rules
This commit is contained in:
Erik Montnemery 2024-03-01 13:07:13 +01:00 committed by GitHub
parent e209ae3d4e
commit 3a5e0c14bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 60 additions and 18 deletions

View File

@ -245,7 +245,11 @@ UPDATE_ENTRY_CONFIG_ENTRY_ATTRS = {
} }
ConfigFlowResult = FlowResult class ConfigFlowResult(FlowResult, total=False):
"""Typed result dict for config flow."""
minor_version: int
version: int
class ConfigEntry: class ConfigEntry:

View File

@ -151,7 +151,6 @@ class FlowResult(TypedDict, total=False):
handler: Required[str] handler: Required[str]
last_step: bool | None last_step: bool | None
menu_options: list[str] | dict[str, str] menu_options: list[str] | dict[str, str]
minor_version: int
options: Mapping[str, Any] options: Mapping[str, Any]
preview: str | None preview: str | None
progress_action: str progress_action: str
@ -164,7 +163,6 @@ class FlowResult(TypedDict, total=False):
translation_domain: str translation_domain: str
type: FlowResultType type: FlowResultType
url: str url: str
version: int
def _map_error_to_schema_errors( def _map_error_to_schema_errors(

View File

@ -494,11 +494,6 @@ _CLASS_MATCH: dict[str, list[ClassTypeHintMatch]] = {
ClassTypeHintMatch( ClassTypeHintMatch(
base_class="ConfigFlow", base_class="ConfigFlow",
matches=[ matches=[
TypeHintMatch(
function_name="async_step123_*",
arg_types={},
return_type=["ConfigFlowResult", "FlowResult"],
),
TypeHintMatch( TypeHintMatch(
function_name="async_get_options_flow", function_name="async_get_options_flow",
arg_types={ arg_types={
@ -511,56 +506,61 @@ _CLASS_MATCH: dict[str, list[ClassTypeHintMatch]] = {
arg_types={ arg_types={
1: "DhcpServiceInfo", 1: "DhcpServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_hassio", function_name="async_step_hassio",
arg_types={ arg_types={
1: "HassioServiceInfo", 1: "HassioServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_homekit", function_name="async_step_homekit",
arg_types={ arg_types={
1: "ZeroconfServiceInfo", 1: "ZeroconfServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_mqtt", function_name="async_step_mqtt",
arg_types={ arg_types={
1: "MqttServiceInfo", 1: "MqttServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_reauth", function_name="async_step_reauth",
arg_types={ arg_types={
1: "Mapping[str, Any]", 1: "Mapping[str, Any]",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_ssdp", function_name="async_step_ssdp",
arg_types={ arg_types={
1: "SsdpServiceInfo", 1: "SsdpServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_usb", function_name="async_step_usb",
arg_types={ arg_types={
1: "UsbServiceInfo", 1: "UsbServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
TypeHintMatch( TypeHintMatch(
function_name="async_step_zeroconf", function_name="async_step_zeroconf",
arg_types={ arg_types={
1: "ZeroconfServiceInfo", 1: "ZeroconfServiceInfo",
}, },
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_*",
arg_types={},
return_type="ConfigFlowResult",
), ),
], ],
), ),
@ -570,7 +570,7 @@ _CLASS_MATCH: dict[str, list[ClassTypeHintMatch]] = {
TypeHintMatch( TypeHintMatch(
function_name="async_step_*", function_name="async_step_*",
arg_types={}, arg_types={},
return_type=["ConfigFlowResult", "FlowResult"], return_type="ConfigFlowResult",
), ),
], ],
), ),

View File

@ -346,7 +346,7 @@ def test_invalid_config_flow_step(
pylint.testutils.MessageTest( pylint.testutils.MessageTest(
msg_id="hass-return-type", msg_id="hass-return-type",
node=func_node, node=func_node,
args=(["ConfigFlowResult", "FlowResult"], "async_step_zeroconf"), args=("ConfigFlowResult", "async_step_zeroconf"),
line=11, line=11,
col_offset=4, col_offset=4,
end_line=11, end_line=11,
@ -356,6 +356,46 @@ def test_invalid_config_flow_step(
type_hint_checker.visit_classdef(class_node) type_hint_checker.visit_classdef(class_node)
def test_invalid_custom_config_flow_step(
linter: UnittestLinter, type_hint_checker: BaseChecker
) -> None:
"""Ensure invalid hints are rejected for ConfigFlow step."""
class_node, func_node, arg_node = astroid.extract_node(
"""
class FlowHandler():
pass
class ConfigFlow(FlowHandler):
pass
class AxisFlowHandler( #@
ConfigFlow, domain=AXIS_DOMAIN
):
async def async_step_axis_specific( #@
self,
device_config: dict #@
):
pass
""",
"homeassistant.components.pylint_test.config_flow",
)
type_hint_checker.visit_module(class_node.parent)
with assert_adds_messages(
linter,
pylint.testutils.MessageTest(
msg_id="hass-return-type",
node=func_node,
args=("ConfigFlowResult", "async_step_axis_specific"),
line=11,
col_offset=4,
end_line=11,
end_col_offset=38,
),
):
type_hint_checker.visit_classdef(class_node)
def test_valid_config_flow_step( def test_valid_config_flow_step(
linter: UnittestLinter, type_hint_checker: BaseChecker linter: UnittestLinter, type_hint_checker: BaseChecker
) -> None: ) -> None: