mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 11:45:09 +02:00
Move pylint decorator plugin and add tests (#126719)
This commit is contained in:
33
pylint/plugins/hass_decorator.py
Normal file
33
pylint/plugins/hass_decorator.py
Normal 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))
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user