Prevent callback decorator on coroutine functions (#126429)

* Prevent callback decorator on async functions

* Adjust

* Adjust

* Adjust components

* Adjust tests

* Rename

* One more

* Adjust

* Adjust again

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
epenet
2024-09-23 02:55:55 +02:00
committed by GitHub
parent 113a792734
commit c759512c70
13 changed files with 22 additions and 20 deletions

View File

@@ -3093,6 +3093,11 @@ class HassTypeHintChecker(BaseChecker):
"hass-consider-usefixtures-decorator",
"Used when an argument type is None and could be a fixture",
),
"W7434": (
"A coroutine function should not be decorated with @callback",
"hass-async-callback-decorator",
"Used when a coroutine function has an invalid @callback decorator",
),
}
options = (
(
@@ -3195,6 +3200,14 @@ class HassTypeHintChecker(BaseChecker):
self._check_function(function_node, match, annotations)
checked_class_methods.add(function_node.name)
def visit_asyncfunctiondef(self, node: nodes.AsyncFunctionDef) -> None:
"""Apply checks on an AsyncFunctionDef node."""
if (
decoratornames := node.decoratornames()
) and "homeassistant.core.callback" in decoratornames:
self.add_message("hass-async-callback-decorator", node=node)
self.visit_functiondef(node)
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
"""Apply relevant type hint checks on a FunctionDef node."""
annotations = _get_all_annotations(node)
@@ -3234,8 +3247,6 @@ class HassTypeHintChecker(BaseChecker):
continue
self._check_function(node, match, annotations)
visit_asyncfunctiondef = visit_functiondef
def _check_function(
self,
node: nodes.FunctionDef,