Move pylint decorator plugin and add tests (#126719)

This commit is contained in:
epenet
2024-09-25 09:41:23 +02:00
committed by GitHub
parent 65abe1c875
commit dff0e2cc9f
5 changed files with 117 additions and 13 deletions

View File

@@ -0,0 +1,33 @@
"""Plugin to check decorators."""
from __future__ import annotations
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.lint import PyLinter
class HassDecoratorChecker(BaseChecker):
"""Checker for decorators."""
name = "hass_decorator"
priority = -1
msgs = {
"W7471": (
"A coroutine function should not be decorated with @callback",
"hass-async-callback-decorator",
"Used when a coroutine function has an invalid @callback decorator",
),
}
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)
def register(linter: PyLinter) -> None:
"""Register the checker."""
linter.register_checker(HassDecoratorChecker(linter))

View File

@@ -3093,11 +3093,6 @@ 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 = (
(
@@ -3200,14 +3195,6 @@ 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)
@@ -3247,6 +3234,8 @@ class HassTypeHintChecker(BaseChecker):
continue
self._check_function(node, match, annotations)
visit_asyncfunctiondef = visit_functiondef
def _check_function(
self,
node: nodes.FunctionDef,