From 029d681d1c1e1af825b445d2d096b27db74715cc Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 13 Feb 2026 14:10:37 +0000 Subject: [PATCH] Add pylint plugin to check constant usage --- pylint/plugins/hass_enforce_constants.py | 57 ++++++++++++++++++++++++ pyproject.toml | 1 + 2 files changed, 58 insertions(+) create mode 100644 pylint/plugins/hass_enforce_constants.py diff --git a/pylint/plugins/hass_enforce_constants.py b/pylint/plugins/hass_enforce_constants.py new file mode 100644 index 00000000000..66a25a3d793 --- /dev/null +++ b/pylint/plugins/hass_enforce_constants.py @@ -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)) diff --git a/pyproject.toml b/pyproject.toml index 55f62b7484f..aef6469a2ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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",