forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhass_constructor.py
51 lines (42 loc) · 1.66 KB
/
hass_constructor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Plugin for constructor definitions."""
from __future__ import annotations
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.lint import PyLinter
class HassConstructorFormatChecker(BaseChecker): # type: ignore[misc]
"""Checker for __init__ definitions."""
name = "hass_constructor"
priority = -1
msgs = {
"W7411": (
'__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: nodes.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, nodes.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))