From bed2dbbbd53a1e7fdc354c9b44bd832fe590d198 Mon Sep 17 00:00:00 2001 From: iluvdata <86066778+iluvdata@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:07:10 -0500 Subject: [PATCH] Add specific pylint checker to async_step_init of repair flows (#190668) --- .../checkers/type_hints/const.py | 10 +- .../checkers/type_hints/models.py | 4 +- .../pylint_home_assistant/const.py | 1 + tests/pylint/type_hints/test_type_hints.py | 115 ++++++++++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) diff --git a/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/const.py b/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/const.py index 6b1c0cb98044..07f3f4053bdd 100644 --- a/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/const.py +++ b/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/const.py @@ -606,10 +606,18 @@ _CLASS_MATCH: dict[str, list[ClassTypeHintMatch]] = { ], ), ], - "repairs": [ + Module.REPAIRS: [ ClassTypeHintMatch( base_class="RepairsFlow", matches=[ + TypeHintMatch( + function_name="async_step_init", + named_arg_types={ + "user_input": ["dict[str, Any] | None", "dict[str, str] | None"] + }, + return_type="RepairsFlowResult", + mandatory=True, + ), TypeHintMatch( function_name="async_step_*", arg_types={}, diff --git a/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/models.py b/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/models.py index 6aab75520eba..9ef5c69836f1 100644 --- a/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/models.py +++ b/packages/pylint-home-assistant/pylint_home_assistant/checkers/type_hints/models.py @@ -14,9 +14,9 @@ class TypeHintMatch: function_name: str return_type: list[str | _Undefined | None] | str | _Undefined | None - arg_types: dict[int, str] | None = None + arg_types: dict[int, str] | dict[int, list[str]] | None = None """arg_types is for positional arguments""" - named_arg_types: dict[str, str] | None = None + named_arg_types: dict[str, str] | dict[str, list[str]] | None = None """named_arg_types is for named or keyword arguments""" kwargs_type: str | None = None """kwargs_type is for the special case `**kwargs`""" diff --git a/packages/pylint-home-assistant/pylint_home_assistant/const.py b/packages/pylint-home-assistant/pylint_home_assistant/const.py index 48ac46f39647..1a51439365ed 100644 --- a/packages/pylint-home-assistant/pylint_home_assistant/const.py +++ b/packages/pylint-home-assistant/pylint_home_assistant/const.py @@ -93,6 +93,7 @@ class Module(StrEnum): DEVICE_TRIGGER = "device_trigger" DIAGNOSTICS = "diagnostics" ENTITY = "entity" + REPAIRS = "repairs" class QualityScaleTier(IntEnum): diff --git a/tests/pylint/type_hints/test_type_hints.py b/tests/pylint/type_hints/test_type_hints.py index 472199cdba9c..e7b1f57bd8bd 100644 --- a/tests/pylint/type_hints/test_type_hints.py +++ b/tests/pylint/type_hints/test_type_hints.py @@ -512,6 +512,121 @@ def test_valid_flow_step( type_hint_checker.visit_classdef(class_node) +@pytest.mark.parametrize( + ("code", "expected_messages_fn"), + [ + ( + """ + class FlowHandler(): + pass + + class RepairsFlow(FlowHandler): + pass + + class AxisRepairsFlowHandler(RepairsFlow): #@ + async def async_step_init( + self, + user_input: dict[str, str] #@ + ) -> RepairsFlowResult: + pass +""", + lambda func_node: [ + pylint.testutils.MessageTest( + msg_id="home-assistant-argument-type", + node=func_node, + args=( + "user_input", + ["dict[str, Any] | None", "dict[str, str] | None"], + "async_step_init", + ), + line=11, + col_offset=8, + end_line=11, + end_col_offset=34, + ), + ], + ), + ( + """ + class FlowHandler(): + pass + + class RepairsFlow(FlowHandler): + pass + + class AxisRepairsFlowHandler(RepairsFlow): #@ + async def async_step_arbitrary( #@ + self, + user_input: dict[str, str] + ) -> FlowResult: + pass +""", + lambda func_node: [ + pylint.testutils.MessageTest( + msg_id="home-assistant-return-type", + node=func_node, + args=("RepairsFlowResult", "async_step_arbitrary"), + line=9, + col_offset=4, + end_line=9, + end_col_offset=34, + ), + ], + ), + ], + ids=["Invalid argument type", "Invalid return type"], +) +def test_invalid_repairs_flow_async_step_init( + linter: UnittestLinter, + type_hint_checker: BaseChecker, + code: str, + expected_messages_fn: Callable[ + [nodes.NodeNG], tuple[pylint.testutils.MessageTest, ...] + ], +) -> None: + """Ensure valid hints are accepted for flow step.""" + class_node, func_node = astroid.extract_node( + code, + "homeassistant.components.pylint_test.repairs", + ) + type_hint_checker.visit_module(class_node.parent) + + with assert_adds_messages(linter, *expected_messages_fn(func_node)): + type_hint_checker.visit_classdef(class_node) + + +def test_valid_repair_flow_step( + linter: UnittestLinter, + type_hint_checker: BaseChecker, +) -> None: + """Ensure valid hints are accepted for repairs flow steps.""" + class_node = astroid.extract_node( + """ + class FlowHandler(): + pass + + class RepairsFlow(FlowHandler): + pass + + class AxisRepairsFlowHandler(RepairsFlow): + async def async_step_init( + self, + user_input: dict[str, Any] | None + ) -> RepairsFlowResult: + pass + async def async_step_arbitrary( + self + ) -> RepairsFlowResult: + pass + """, + "homeassistant.components.pylint_test.repairs", + ) + type_hint_checker.visit_module(class_node.parent) + + with assert_no_messages(linter): + type_hint_checker.visit_classdef(class_node) + + def test_invalid_config_flow_async_get_options_flow( linter: UnittestLinter, type_hint_checker: BaseChecker ) -> None: