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

View file

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