Skip to content

Commit cc7680b

Browse files
authored
Adjust pylint plugin to enforce diagnostics type hints (home-assistant#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 <[email protected]>
1 parent b97cd3c commit cc7680b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

homeassistant/components/diagnostics/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from http import HTTPStatus
55
import json
66
import logging
7-
from typing import Protocol
7+
from typing import Any, Protocol
88

99
from aiohttp import web
1010
import voluptuous as vol
@@ -51,12 +51,12 @@ class DiagnosticsProtocol(Protocol):
5151

5252
async def async_get_config_entry_diagnostics(
5353
self, hass: HomeAssistant, config_entry: ConfigEntry
54-
) -> dict:
54+
) -> Any:
5555
"""Return diagnostics for a config entry."""
5656

5757
async def async_get_device_diagnostics(
5858
self, hass: HomeAssistant, config_entry: ConfigEntry, device: DeviceEntry
59-
) -> dict:
59+
) -> Any:
6060
"""Return diagnostics for a device."""
6161

6262

@@ -125,7 +125,7 @@ def handle_get(
125125

126126
async def _async_get_json_file_response(
127127
hass: HomeAssistant,
128-
data: dict | list,
128+
data: Any,
129129
filename: str,
130130
domain: str,
131131
d_type: DiagnosticsType,

pylint/plugins/hass_enforce_type_hints.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pylint.lint import PyLinter
1111

1212
from homeassistant.const import Platform
13+
from homeassistant.helpers.typing import UNDEFINED
1314

1415

1516
@dataclass
@@ -39,9 +40,9 @@ class TypeHintMatch:
3940
f"^homeassistant\\.components\\.\\w+\\.({'|'.join([platform.value for platform in Platform])})$"
4041
),
4142
# device_tracker matches only in the package root (device_tracker.py)
42-
"device_tracker": re.compile(
43-
f"^homeassistant\\.components\\.\\w+\\.({Platform.DEVICE_TRACKER.value})$"
44-
),
43+
"device_tracker": re.compile(r"^homeassistant\.components\.\w+\.(device_tracker)$"),
44+
# diagnostics matches only in the package root (diagnostics.py)
45+
"diagnostics": re.compile(r"^homeassistant\.components\.\w+\.(diagnostics)$"),
4546
}
4647

4748
_METHOD_MATCH: list[TypeHintMatch] = [
@@ -171,11 +172,33 @@ class TypeHintMatch:
171172
},
172173
return_type=["DeviceScanner", "DeviceScanner | None"],
173174
),
175+
TypeHintMatch(
176+
module_filter=_MODULE_FILTERS["diagnostics"],
177+
function_name="async_get_config_entry_diagnostics",
178+
arg_types={
179+
0: "HomeAssistant",
180+
1: "ConfigEntry",
181+
},
182+
return_type=UNDEFINED,
183+
),
184+
TypeHintMatch(
185+
module_filter=_MODULE_FILTERS["diagnostics"],
186+
function_name="async_get_device_diagnostics",
187+
arg_types={
188+
0: "HomeAssistant",
189+
1: "ConfigEntry",
190+
2: "DeviceEntry",
191+
},
192+
return_type=UNDEFINED,
193+
),
174194
]
175195

176196

177197
def _is_valid_type(expected_type: list[str] | str | None, node: astroid.NodeNG) -> bool:
178198
"""Check the argument node against the expected type."""
199+
if expected_type is UNDEFINED:
200+
return True
201+
179202
if isinstance(expected_type, list):
180203
for expected_type_item in expected_type:
181204
if _is_valid_type(expected_type_item, node):

0 commit comments

Comments
 (0)