From 3c7a9272fa952cf317267b6dc55f2c7fae608381 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 8 Jan 2024 10:09:48 +0100 Subject: [PATCH] Enable strict typing for intent (#107282) --- .strict-typing | 1 + homeassistant/components/intent/__init__.py | 23 ++++++++++++++++----- homeassistant/helpers/intent.py | 6 +++--- mypy.ini | 10 +++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.strict-typing b/.strict-typing index 501779c4700..4a1876bbadf 100644 --- a/.strict-typing +++ b/.strict-typing @@ -228,6 +228,7 @@ homeassistant.components.input_button.* homeassistant.components.input_select.* homeassistant.components.input_text.* homeassistant.components.integration.* +homeassistant.components.intent.* homeassistant.components.ipp.* homeassistant.components.iqvia.* homeassistant.components.islamic_prayer_times.* diff --git a/homeassistant/components/intent/__init__.py b/homeassistant/components/intent/__init__.py index 306f169106b..5756b78b4de 100644 --- a/homeassistant/components/intent/__init__.py +++ b/homeassistant/components/intent/__init__.py @@ -1,6 +1,10 @@ """The Intent integration.""" -import logging +from __future__ import annotations +import logging +from typing import Any, Protocol + +from aiohttp import web import voluptuous as vol from homeassistant.components import http @@ -69,6 +73,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True +class IntentPlatformProtocol(Protocol): + """Define the format that intent platforms can have.""" + + async def async_setup_intents(self, hass: HomeAssistant) -> None: + """Set up platform intents.""" + + class OnOffIntentHandler(intent.ServiceIntentHandler): """Intent handler for on/off that handles covers too.""" @@ -249,7 +260,9 @@ class NevermindIntentHandler(intent.IntentHandler): return intent_obj.create_response() -async def _async_process_intent(hass: HomeAssistant, domain: str, platform): +async def _async_process_intent( + hass: HomeAssistant, domain: str, platform: IntentPlatformProtocol +) -> None: """Process the intents of an integration.""" await platform.async_setup_intents(hass) @@ -268,9 +281,9 @@ class IntentHandleView(http.HomeAssistantView): } ) ) - async def post(self, request, data): + async def post(self, request: web.Request, data: dict[str, Any]) -> web.Response: """Handle intent with name/data.""" - hass = request.app["hass"] + hass: HomeAssistant = request.app["hass"] language = hass.config.language try: @@ -286,7 +299,7 @@ class IntentHandleView(http.HomeAssistantView): intent_result.async_set_speech(str(err)) if intent_result is None: - intent_result = intent.IntentResponse(language=language) + intent_result = intent.IntentResponse(language=language) # type: ignore[unreachable] intent_result.async_set_speech("Sorry, I couldn't handle that") return self.json(intent_result) diff --git a/homeassistant/helpers/intent.py b/homeassistant/helpers/intent.py index 056f972e7f7..ee326558467 100644 --- a/homeassistant/helpers/intent.py +++ b/homeassistant/helpers/intent.py @@ -2,7 +2,7 @@ from __future__ import annotations import asyncio -from collections.abc import Collection, Iterable +from collections.abc import Collection, Coroutine, Iterable import dataclasses from dataclasses import dataclass from enum import Enum @@ -451,7 +451,7 @@ class ServiceIntentHandler(IntentHandler): else: speech_name = states[0].name - service_coros = [] + service_coros: list[Coroutine[Any, Any, None]] = [] for state in states: service_coros.append(self.async_call_service(intent_obj, state)) @@ -507,7 +507,7 @@ class ServiceIntentHandler(IntentHandler): ) ) - async def _run_then_background(self, task: asyncio.Task) -> None: + async def _run_then_background(self, task: asyncio.Task[Any]) -> None: """Run task with timeout to (hopefully) catch validation errors. After the timeout the task will continue to run in the background. diff --git a/mypy.ini b/mypy.ini index 3315704745f..d0bebf791c3 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2041,6 +2041,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.intent.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.ipp.*] check_untyped_defs = true disallow_incomplete_defs = true