Skip to content

Commit

Permalink
Remove config flow specifics from FlowResult (home-assistant#111932)
Browse files Browse the repository at this point in the history
* Remove config flow specifics from FlowResult

* Improve docstring

* Update pylint rules
  • Loading branch information
emontnemery authored Mar 1, 2024
1 parent e209ae3 commit 3a5e0c1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
6 changes: 5 additions & 1 deletion homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ class OperationNotAllowed(ConfigError):
}


ConfigFlowResult = FlowResult
class ConfigFlowResult(FlowResult, total=False):
"""Typed result dict for config flow."""

minor_version: int
version: int


class ConfigEntry:
Expand Down
2 changes: 0 additions & 2 deletions homeassistant/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class FlowResult(TypedDict, total=False):
handler: Required[str]
last_step: bool | None
menu_options: list[str] | dict[str, str]
minor_version: int
options: Mapping[str, Any]
preview: str | None
progress_action: str
Expand All @@ -164,7 +163,6 @@ class FlowResult(TypedDict, total=False):
translation_domain: str
type: FlowResultType
url: str
version: int


def _map_error_to_schema_errors(
Expand Down
28 changes: 14 additions & 14 deletions pylint/plugins/hass_enforce_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,6 @@ class ClassTypeHintMatch:
ClassTypeHintMatch(
base_class="ConfigFlow",
matches=[
TypeHintMatch(
function_name="async_step123_*",
arg_types={},
return_type=["ConfigFlowResult", "FlowResult"],
),
TypeHintMatch(
function_name="async_get_options_flow",
arg_types={
Expand All @@ -511,56 +506,61 @@ class ClassTypeHintMatch:
arg_types={
1: "DhcpServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_hassio",
arg_types={
1: "HassioServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_homekit",
arg_types={
1: "ZeroconfServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_mqtt",
arg_types={
1: "MqttServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_reauth",
arg_types={
1: "Mapping[str, Any]",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_ssdp",
arg_types={
1: "SsdpServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_usb",
arg_types={
1: "UsbServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_zeroconf",
arg_types={
1: "ZeroconfServiceInfo",
},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
TypeHintMatch(
function_name="async_step_*",
arg_types={},
return_type="ConfigFlowResult",
),
],
),
Expand All @@ -570,7 +570,7 @@ class ClassTypeHintMatch:
TypeHintMatch(
function_name="async_step_*",
arg_types={},
return_type=["ConfigFlowResult", "FlowResult"],
return_type="ConfigFlowResult",
),
],
),
Expand Down
42 changes: 41 additions & 1 deletion tests/pylint/test_enforce_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ async def async_step_zeroconf( #@
pylint.testutils.MessageTest(
msg_id="hass-return-type",
node=func_node,
args=(["ConfigFlowResult", "FlowResult"], "async_step_zeroconf"),
args=("ConfigFlowResult", "async_step_zeroconf"),
line=11,
col_offset=4,
end_line=11,
Expand All @@ -356,6 +356,46 @@ async def async_step_zeroconf( #@
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(
linter: UnittestLinter, type_hint_checker: BaseChecker
) -> None:
Expand Down

0 comments on commit 3a5e0c1

Please sign in to comment.