Ensure __init__ return type is None (#87521)
* Ensure __init__ return type is None * Remove hass-constructor plugin * Simplify * Replace dict with list * Cleaup pyproject.toml * Move is_method outside loop * Adjust looppull/87553/head
parent
939eef3b28
commit
de807fa782
|
@ -1,51 +0,0 @@
|
|||
"""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:
|
||||
"""Check for improperly typed `__init__` definitions."""
|
||||
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))
|
|
@ -83,6 +83,13 @@ _TYPE_HINT_MATCHERS.update(
|
|||
|
||||
_MODULE_REGEX: re.Pattern[str] = re.compile(r"^homeassistant\.components\.\w+(\.\w+)?$")
|
||||
|
||||
_METHOD_MATCH: list[TypeHintMatch] = [
|
||||
TypeHintMatch(
|
||||
function_name="__init__",
|
||||
return_type=None,
|
||||
),
|
||||
]
|
||||
|
||||
_FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = {
|
||||
"__init__": [
|
||||
TypeHintMatch(
|
||||
|
@ -2973,9 +2980,13 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
|
|||
args=(arg_name, expected_type, node.name),
|
||||
)
|
||||
|
||||
# Check function matchers.
|
||||
for match in self._function_matchers:
|
||||
if not match.need_to_check_function(node) or node.is_method():
|
||||
# Check method or function matchers.
|
||||
if node.is_method():
|
||||
matchers = _METHOD_MATCH
|
||||
else:
|
||||
matchers = self._function_matchers
|
||||
for match in matchers:
|
||||
if not match.need_to_check_function(node):
|
||||
continue
|
||||
self._check_function(node, match, annotations)
|
||||
|
||||
|
|
|
@ -112,7 +112,6 @@ init-hook = """\
|
|||
load-plugins = [
|
||||
"pylint.extensions.code_style",
|
||||
"pylint.extensions.typing",
|
||||
"hass_constructor",
|
||||
"hass_enforce_type_hints",
|
||||
"hass_imports",
|
||||
"hass_logger",
|
||||
|
|
Loading…
Reference in New Issue