1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Add pylint plugin to check constant usage

This commit is contained in:
epenet
2026-02-13 14:10:37 +00:00
parent 4e71a38e31
commit 029d681d1c
2 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
"""Plugin to enforce type hints on specific functions."""
from __future__ import annotations
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.lint import PyLinter
class HassEnforceConstantsChecker(BaseChecker):
"""Checker for use of constants."""
name = "hass_enforce_constants"
priority = -1
msgs = {
"W7491": (
"Argument %s should be a DOMAIN constant in %s",
"hass-argument-domain-constant",
"Used when method argument should be a DOMAIN constant.",
),
}
_in_test_module: bool
def visit_module(self, node: nodes.Module) -> None:
"""Visit Module node."""
self._in_test_module = node.name.startswith("tests.")
def visit_call(self, node: nodes.Call) -> None:
"""Visit Call node."""
if not self._in_test_module:
return
if isinstance(node.func, nodes.Name):
if node.func.name == "async_setup_component":
self._ensure_domain_argument(node, node.args[1])
def _ensure_domain_argument(
self, call_node: nodes.Call, arg_node: nodes.Argument
) -> None:
if isinstance(arg_node, nodes.Attribute) and arg_node.attrname.endswith(
"DOMAIN"
):
return
if isinstance(arg_node, nodes.Name) and arg_node.name.endswith("DOMAIN"):
return
self.add_message(
"hass-argument-domain-constant",
node=arg_node,
args=(arg_node.as_string(), call_node.func.as_string()),
)
def register(linter: PyLinter) -> None:
"""Register the checker."""
linter.register_checker(HassEnforceConstantsChecker(linter))

View File

@@ -125,6 +125,7 @@ load-plugins = [
"hass_async_load_fixtures",
"hass_decorator",
"hass_enforce_class_module",
"hass_enforce_constants",
"hass_enforce_greek_micro_char",
"hass_enforce_sorted_platforms",
"hass_enforce_super_call",