Adjust pylint plugin to enforce diagnostics type hints (#64976)

* Adjust pylint plugin to enforce diagnostics type hints

* Adjust return_type

* Set return_type to UNDEFINED

* Use Any for the expected data

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/65567/head
epenet 2022-02-03 19:22:43 +01:00 committed by GitHub
parent b97cd3ce93
commit cc7680b0c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from http import HTTPStatus
import json
import logging
from typing import Protocol
from typing import Any, Protocol
from aiohttp import web
import voluptuous as vol
@ -51,12 +51,12 @@ class DiagnosticsProtocol(Protocol):
async def async_get_config_entry_diagnostics(
self, hass: HomeAssistant, config_entry: ConfigEntry
) -> dict:
) -> Any:
"""Return diagnostics for a config entry."""
async def async_get_device_diagnostics(
self, hass: HomeAssistant, config_entry: ConfigEntry, device: DeviceEntry
) -> dict:
) -> Any:
"""Return diagnostics for a device."""
@ -125,7 +125,7 @@ def handle_get(
async def _async_get_json_file_response(
hass: HomeAssistant,
data: dict | list,
data: Any,
filename: str,
domain: str,
d_type: DiagnosticsType,

View File

@ -10,6 +10,7 @@ from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter
from homeassistant.const import Platform
from homeassistant.helpers.typing import UNDEFINED
@dataclass
@ -39,9 +40,9 @@ _MODULE_FILTERS: dict[str, re.Pattern] = {
f"^homeassistant\\.components\\.\\w+\\.({'|'.join([platform.value for platform in Platform])})$"
),
# device_tracker matches only in the package root (device_tracker.py)
"device_tracker": re.compile(
f"^homeassistant\\.components\\.\\w+\\.({Platform.DEVICE_TRACKER.value})$"
),
"device_tracker": re.compile(r"^homeassistant\.components\.\w+\.(device_tracker)$"),
# diagnostics matches only in the package root (diagnostics.py)
"diagnostics": re.compile(r"^homeassistant\.components\.\w+\.(diagnostics)$"),
}
_METHOD_MATCH: list[TypeHintMatch] = [
@ -171,11 +172,33 @@ _METHOD_MATCH: list[TypeHintMatch] = [
},
return_type=["DeviceScanner", "DeviceScanner | None"],
),
TypeHintMatch(
module_filter=_MODULE_FILTERS["diagnostics"],
function_name="async_get_config_entry_diagnostics",
arg_types={
0: "HomeAssistant",
1: "ConfigEntry",
},
return_type=UNDEFINED,
),
TypeHintMatch(
module_filter=_MODULE_FILTERS["diagnostics"],
function_name="async_get_device_diagnostics",
arg_types={
0: "HomeAssistant",
1: "ConfigEntry",
2: "DeviceEntry",
},
return_type=UNDEFINED,
),
]
def _is_valid_type(expected_type: list[str] | str | None, node: astroid.NodeNG) -> bool:
"""Check the argument node against the expected type."""
if expected_type is UNDEFINED:
return True
if isinstance(expected_type, list):
for expected_type_item in expected_type:
if _is_valid_type(expected_type_item, node):