1
mirror of https://github.com/home-assistant/core synced 2024-08-06 09:34:49 +02:00
ha-core/pylint/plugins/hass_constructor.py
Ruslan Sayfutdinov 016abda12e
Pylint plugin to check that relative imports are used (#50937)
* Pylint plugin to check that relative imports are used

* Fix existing sites

* Update description message

* Fix typo
2021-05-22 09:15:30 +01:00

53 lines
1.7 KiB
Python

"""Plugin for constructor definitions."""
from astroid import Const, FunctionDef
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter
class HassConstructorFormatChecker(BaseChecker): # type: ignore[misc]
"""Checker for __init__ definitions."""
__implements__ = IAstroidChecker
name = "hass_constructor"
priority = -1
msgs = {
"W0006": (
'__init__ should have explicit return type "None"',
"hass-constructor-return",
"Used when __init__ has all arguments typed "
"but doesn't have return type declared",
),
}
options = ()
def visit_functiondef(self, node: FunctionDef) -> None:
"""Called when a FunctionDef node is visited."""
if not node.is_method() or node.name != "__init__":
return
# Check that all arguments are annotated.
# The first argument is "self".
args = node.args
annotations = (
args.posonlyargs_annotations
+ args.annotations
+ args.kwonlyargs_annotations
)[1:]
if args.vararg is not None:
annotations.append(args.varargannotation)
if args.kwarg is not None:
annotations.append(args.kwargannotation)
if not annotations or None in annotations:
return
# Check that return type is specified and it is "None".
if not isinstance(node.returns, Const) or node.returns.value is not None:
self.add_message("hass-constructor-return", node=node)
def register(linter: PyLinter) -> None:
"""Register the checker."""
linter.register_checker(HassConstructorFormatChecker(linter))