Pylint plugin to check __init__ return type (#50868)

* Pylint plugin to check __init__ return type

* Support *args add **kwargs, type hints

* Use 'in' instead of 'any()'

* Fix last few places
This commit is contained in:
Ruslan Sayfutdinov 2021-05-20 18:00:10 +01:00 committed by GitHub
parent fd2e640c74
commit 0e7409e617
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 32 deletions

View file

@ -1,18 +1,18 @@
"""Plugin for logger invocations."""
import astroid
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter
LOGGER_NAMES = ("LOGGER", "_LOGGER")
LOG_LEVEL_ALLOWED_LOWER_START = ("debug",)
# This is our checker class.
# Checkers should always inherit from `BaseChecker`.
class HassLoggerFormatChecker(BaseChecker):
"""Add class member attributes to the class locals dictionary."""
class HassLoggerFormatChecker(BaseChecker): # type: ignore[misc]
"""Checker for logger invocations."""
__implements__ = IAstroidChecker
# The name defines a custom section of the config for this checker.
name = "hass_logger"
priority = -1
msgs = {
@ -27,24 +27,10 @@ class HassLoggerFormatChecker(BaseChecker):
"All logger messages must start with a capital letter",
),
}
options = (
(
"hass-logger",
{
"default": "properties",
"help": (
"Validate _LOGGER or LOGGER messages conform to Home Assistant standards."
),
},
),
)
options = ()
def visit_call(self, node):
"""Called when a :class:`.astroid.node_classes.Call` node is visited.
See :mod:`astroid` for the description of available nodes.
:param node: The node to check.
:type node: astroid.node_classes.Call
"""
def visit_call(self, node: astroid.Call) -> None:
"""Called when a Call node is visited."""
if not isinstance(node.func, astroid.Attribute) or not isinstance(
node.func.expr, astroid.Name
):
@ -67,19 +53,16 @@ class HassLoggerFormatChecker(BaseChecker):
return
if log_message[-1] == ".":
self.add_message("hass-logger-period", args=node.args, node=node)
self.add_message("hass-logger-period", node=node)
if (
isinstance(node.func.attrname, str)
and node.func.attrname not in LOG_LEVEL_ALLOWED_LOWER_START
and log_message[0].upper() != log_message[0]
):
self.add_message("hass-logger-capital", args=node.args, node=node)
self.add_message("hass-logger-capital", node=node)
def register(linter):
"""This required method auto registers the checker.
:param linter: The linter to register the checker to.
:type linter: pylint.lint.PyLinter
"""
def register(linter: PyLinter) -> None:
"""Register the checker."""
linter.register_checker(HassLoggerFormatChecker(linter))