From f178467b0e716b19c9e049c85e9878d8ba84b3f8 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:43:18 +0200 Subject: [PATCH] Add type hints for TTS test fixtures (#118704) --- pylint/plugins/hass_enforce_type_hints.py | 5 +++ tests/components/assist_pipeline/conftest.py | 3 +- tests/components/cloud/conftest.py | 5 +-- tests/components/conftest.py | 12 ++++--- tests/components/esphome/conftest.py | 3 +- tests/components/google_translate/test_tts.py | 5 +-- tests/components/marytts/test_tts.py | 3 +- tests/components/microsoft/test_tts.py | 3 +- tests/components/tts/common.py | 12 ++++--- tests/components/tts/conftest.py | 6 ++-- tests/components/tts/test_init.py | 35 ++++++++++--------- tests/components/tts/test_legacy.py | 4 ++- tests/components/voicerss/test_tts.py | 6 ++-- tests/components/voip/test_voip.py | 3 +- tests/components/wyoming/conftest.py | 3 +- tests/components/yandextts/test_tts.py | 6 ++-- 16 files changed, 72 insertions(+), 42 deletions(-) diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index e99c5c1ed39..58baeb6d1cd 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -130,6 +130,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "hass_supervisor_access_token": "str", "hass_supervisor_user": "MockUser", "hass_ws_client": "WebSocketGenerator", + "init_tts_cache_dir_side_effect": "Any", "issue_registry": "IssueRegistry", "legacy_auth": "LegacyApiPasswordAuthProvider", "local_auth": "HassAuthProvider", @@ -141,6 +142,9 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "mock_get_source_ip": "_patch", "mock_hass_config": "None", "mock_hass_config_yaml": "None", + "mock_tts_cache_dir": "Path", + "mock_tts_get_cache_files": "MagicMock", + "mock_tts_init_cache_dir": "MagicMock", "mock_zeroconf": "MagicMock", "mqtt_client_mock": "MqttMockPahoClient", "mqtt_mock": "MqttMockHAClient", @@ -153,6 +157,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "stub_blueprint_populate": "None", "tmp_path": "Path", "tmpdir": "py.path.local", + "tts_mutagen_mock": "MagicMock", "unused_tcp_port_factory": "Callable[[], int]", "unused_udp_port_factory": "Callable[[], int]", } diff --git a/tests/components/assist_pipeline/conftest.py b/tests/components/assist_pipeline/conftest.py index f4c4ddf1730..69d44341f4a 100644 --- a/tests/components/assist_pipeline/conftest.py +++ b/tests/components/assist_pipeline/conftest.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import AsyncIterable, Generator +from pathlib import Path from typing import Any from unittest.mock import AsyncMock @@ -34,7 +35,7 @@ _TRANSCRIPT = "test transcript" @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/cloud/conftest.py b/tests/components/cloud/conftest.py index 0147556a888..063aa702c88 100644 --- a/tests/components/cloud/conftest.py +++ b/tests/components/cloud/conftest.py @@ -1,6 +1,7 @@ """Fixtures for cloud tests.""" from collections.abc import AsyncGenerator, Callable, Coroutine +from pathlib import Path from typing import Any from unittest.mock import DEFAULT, MagicMock, PropertyMock, patch @@ -180,13 +181,13 @@ def set_cloud_prefs_fixture( @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir @pytest.fixture(autouse=True) -def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock): +def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None: """Mock writing tags.""" diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 8bbb3b83c22..ee5806dd1a4 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import Callable, Generator +from pathlib import Path from typing import TYPE_CHECKING, Any from unittest.mock import MagicMock, patch @@ -59,7 +60,7 @@ def stub_blueprint_populate_fixture() -> Generator[None, None, None]: # TTS test fixtures @pytest.fixture(name="mock_tts_get_cache_files") -def mock_tts_get_cache_files_fixture(): +def mock_tts_get_cache_files_fixture() -> Generator[MagicMock, None, None]: """Mock the list TTS cache function.""" from tests.components.tts.common import mock_tts_get_cache_files_fixture_helper @@ -88,8 +89,11 @@ def init_tts_cache_dir_side_effect_fixture() -> Any: @pytest.fixture(name="mock_tts_cache_dir") def mock_tts_cache_dir_fixture( - tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request -): + tmp_path: Path, + mock_tts_init_cache_dir: MagicMock, + mock_tts_get_cache_files: MagicMock, + request: pytest.FixtureRequest, +) -> Generator[Path, None, None]: """Mock the TTS cache dir with empty dir.""" from tests.components.tts.common import mock_tts_cache_dir_fixture_helper @@ -99,7 +103,7 @@ def mock_tts_cache_dir_fixture( @pytest.fixture(name="tts_mutagen_mock") -def tts_mutagen_mock_fixture(): +def tts_mutagen_mock_fixture() -> Generator[MagicMock, None, None]: """Mock writing tags.""" from tests.components.tts.common import tts_mutagen_mock_fixture_helper diff --git a/tests/components/esphome/conftest.py b/tests/components/esphome/conftest.py index 7b9b050ddb3..91d4f140b12 100644 --- a/tests/components/esphome/conftest.py +++ b/tests/components/esphome/conftest.py @@ -5,6 +5,7 @@ from __future__ import annotations import asyncio from asyncio import Event from collections.abc import Awaitable, Callable +from pathlib import Path from typing import Any from unittest.mock import AsyncMock, Mock, patch @@ -57,7 +58,7 @@ async def load_homeassistant(hass) -> None: @pytest.fixture(autouse=True) -def mock_tts(mock_tts_cache_dir): +def mock_tts(mock_tts_cache_dir: Path) -> None: """Auto mock the tts cache.""" diff --git a/tests/components/google_translate/test_tts.py b/tests/components/google_translate/test_tts.py index a9a80e2e8e6..18fd6a24d3b 100644 --- a/tests/components/google_translate/test_tts.py +++ b/tests/components/google_translate/test_tts.py @@ -4,6 +4,7 @@ from __future__ import annotations from collections.abc import Generator from http import HTTPStatus +from pathlib import Path from typing import Any from unittest.mock import MagicMock, patch @@ -28,12 +29,12 @@ from tests.typing import ClientSessionGenerator @pytest.fixture(autouse=True) -def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock): +def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None: """Mock writing tags.""" @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/marytts/test_tts.py b/tests/components/marytts/test_tts.py index 953c66f58d1..75784bb56c5 100644 --- a/tests/components/marytts/test_tts.py +++ b/tests/components/marytts/test_tts.py @@ -2,6 +2,7 @@ from http import HTTPStatus import io +from pathlib import Path from unittest.mock import patch import wave @@ -33,7 +34,7 @@ def get_empty_wav() -> bytes: @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/microsoft/test_tts.py b/tests/components/microsoft/test_tts.py index 9ee915c99b6..94d77955f52 100644 --- a/tests/components/microsoft/test_tts.py +++ b/tests/components/microsoft/test_tts.py @@ -1,6 +1,7 @@ """Tests for Microsoft text-to-speech.""" from http import HTTPStatus +from pathlib import Path from unittest.mock import patch from pycsspeechtts import pycsspeechtts @@ -24,7 +25,7 @@ from tests.typing import ClientSessionGenerator @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/tts/common.py b/tests/components/tts/common.py index 5bdc156eacc..87a9993c72a 100644 --- a/tests/components/tts/common.py +++ b/tests/components/tts/common.py @@ -4,6 +4,7 @@ from __future__ import annotations from collections.abc import Generator from http import HTTPStatus +from pathlib import Path from typing import Any from unittest.mock import MagicMock, patch @@ -41,7 +42,7 @@ SUPPORT_LANGUAGES = ["de_CH", "de_DE", "en_GB", "en_US"] TEST_DOMAIN = "test" -def mock_tts_get_cache_files_fixture_helper(): +def mock_tts_get_cache_files_fixture_helper() -> Generator[MagicMock, None, None]: """Mock the list TTS cache function.""" with patch( "homeassistant.components.tts._get_cache_files", return_value={} @@ -66,8 +67,11 @@ def init_tts_cache_dir_side_effect_fixture_helper() -> Any: def mock_tts_cache_dir_fixture_helper( - tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request -): + tmp_path: Path, + mock_tts_init_cache_dir: MagicMock, + mock_tts_get_cache_files: MagicMock, + request: pytest.FixtureRequest, +) -> Generator[Path, None, None]: """Mock the TTS cache dir with empty dir.""" mock_tts_init_cache_dir.return_value = str(tmp_path) @@ -88,7 +92,7 @@ def mock_tts_cache_dir_fixture_helper( pytest.fail("Test failed, see log for details") -def tts_mutagen_mock_fixture_helper(): +def tts_mutagen_mock_fixture_helper() -> Generator[MagicMock, None, None]: """Mock writing tags.""" with patch( "homeassistant.components.tts.SpeechManager.write_tags", diff --git a/tests/components/tts/conftest.py b/tests/components/tts/conftest.py index a8bdeea5545..7ada92f6088 100644 --- a/tests/components/tts/conftest.py +++ b/tests/components/tts/conftest.py @@ -4,6 +4,8 @@ From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-info """ from collections.abc import Generator +from pathlib import Path +from unittest.mock import MagicMock import pytest @@ -37,13 +39,13 @@ def pytest_runtest_makereport(item, call): @pytest.fixture(autouse=True, name="mock_tts_cache_dir") -def mock_tts_cache_dir_fixture_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_fixture_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir @pytest.fixture(autouse=True) -def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock): +def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None: """Mock writing tags.""" diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index 7d308ec0b23..e0354170b06 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -2,6 +2,7 @@ import asyncio from http import HTTPStatus +from pathlib import Path from typing import Any from unittest.mock import MagicMock, patch @@ -187,7 +188,7 @@ async def test_setup_component_no_access_cache_folder( ) async def test_service( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -248,7 +249,7 @@ async def test_service( ) async def test_service_default_language( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -309,7 +310,7 @@ async def test_service_default_language( ) async def test_service_default_special_language( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -366,7 +367,7 @@ async def test_service_default_special_language( ) async def test_service_language( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -423,7 +424,7 @@ async def test_service_language( ) async def test_service_wrong_language( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -477,7 +478,7 @@ async def test_service_wrong_language( ) async def test_service_options( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -561,7 +562,7 @@ class MockEntityWithDefaults(MockTTSEntity): ) async def test_service_default_options( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -629,7 +630,7 @@ async def test_service_default_options( ) async def test_merge_default_service_options( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -696,7 +697,7 @@ async def test_merge_default_service_options( ) async def test_service_wrong_options( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -752,7 +753,7 @@ async def test_service_wrong_options( ) async def test_service_clear_cache( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -814,7 +815,7 @@ async def test_service_clear_cache( async def test_service_receive_voice( hass: HomeAssistant, hass_client: ClientSessionGenerator, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -886,7 +887,7 @@ async def test_service_receive_voice( async def test_service_receive_voice_german( hass: HomeAssistant, hass_client: ClientSessionGenerator, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -994,7 +995,7 @@ async def test_web_view_wrong_filename( ) async def test_service_without_cache( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, setup: str, tts_service: str, service_data: dict[str, Any], @@ -1042,7 +1043,7 @@ class MockEntityBoom(MockTTSEntity): @pytest.mark.parametrize("mock_provider", [MockProviderBoom(DEFAULT_LANG)]) async def test_setup_legacy_cache_dir( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, mock_provider: MockProvider, ) -> None: """Set up a TTS platform with cache and call service without cache.""" @@ -1078,7 +1079,7 @@ async def test_setup_legacy_cache_dir( @pytest.mark.parametrize("mock_tts_entity", [MockEntityBoom(DEFAULT_LANG)]) async def test_setup_cache_dir( hass: HomeAssistant, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, mock_tts_entity: MockTTSEntity, ) -> None: """Set up a TTS platform with cache and call service without cache.""" @@ -1185,7 +1186,7 @@ async def test_service_get_tts_error( async def test_load_cache_legacy_retrieve_without_mem_cache( hass: HomeAssistant, mock_provider: MockProvider, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, hass_client: ClientSessionGenerator, ) -> None: """Set up component and load cache and get without mem cache.""" @@ -1211,7 +1212,7 @@ async def test_load_cache_legacy_retrieve_without_mem_cache( async def test_load_cache_retrieve_without_mem_cache( hass: HomeAssistant, mock_tts_entity: MockTTSEntity, - mock_tts_cache_dir, + mock_tts_cache_dir: Path, hass_client: ClientSessionGenerator, ) -> None: """Set up component and load cache and get without mem cache.""" diff --git a/tests/components/tts/test_legacy.py b/tests/components/tts/test_legacy.py index 59194f50d93..05bb6dec10f 100644 --- a/tests/components/tts/test_legacy.py +++ b/tests/components/tts/test_legacy.py @@ -2,6 +2,8 @@ from __future__ import annotations +from pathlib import Path + import pytest from homeassistant.components.media_player import ( @@ -139,7 +141,7 @@ async def test_platform_setup_with_error( async def test_service_without_cache_config( - hass: HomeAssistant, mock_tts_cache_dir, mock_tts + hass: HomeAssistant, mock_tts_cache_dir: Path, mock_tts ) -> None: """Set up a TTS platform without cache.""" calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) diff --git a/tests/components/voicerss/test_tts.py b/tests/components/voicerss/test_tts.py index d1e7ba3c62f..1a2ad002586 100644 --- a/tests/components/voicerss/test_tts.py +++ b/tests/components/voicerss/test_tts.py @@ -1,6 +1,8 @@ """The tests for the VoiceRSS speech platform.""" from http import HTTPStatus +from pathlib import Path +from unittest.mock import MagicMock import pytest @@ -29,12 +31,12 @@ FORM_DATA = { @pytest.fixture(autouse=True) -def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock): +def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None: """Mock writing tags.""" @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/voip/test_voip.py b/tests/components/voip/test_voip.py index f5c5fde2518..6c292241237 100644 --- a/tests/components/voip/test_voip.py +++ b/tests/components/voip/test_voip.py @@ -2,6 +2,7 @@ import asyncio import io +from pathlib import Path import time from unittest.mock import AsyncMock, Mock, patch import wave @@ -18,7 +19,7 @@ _MEDIA_ID = "12345" @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/wyoming/conftest.py b/tests/components/wyoming/conftest.py index 4be12312c7a..4ba0c6312cb 100644 --- a/tests/components/wyoming/conftest.py +++ b/tests/components/wyoming/conftest.py @@ -1,6 +1,7 @@ """Common fixtures for the Wyoming tests.""" from collections.abc import Generator +from pathlib import Path from unittest.mock import AsyncMock, patch import pytest @@ -18,7 +19,7 @@ from tests.common import MockConfigEntry @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir diff --git a/tests/components/yandextts/test_tts.py b/tests/components/yandextts/test_tts.py index 6a4b7e11ce6..496c187469a 100644 --- a/tests/components/yandextts/test_tts.py +++ b/tests/components/yandextts/test_tts.py @@ -1,6 +1,8 @@ """The tests for the Yandex SpeechKit speech platform.""" from http import HTTPStatus +from pathlib import Path +from unittest.mock import MagicMock import pytest @@ -22,12 +24,12 @@ URL = "https://tts.voicetech.yandex.net/generate?" @pytest.fixture(autouse=True) -def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock): +def tts_mutagen_mock_fixture_autouse(tts_mutagen_mock: MagicMock) -> None: """Mock writing tags.""" @pytest.fixture(autouse=True) -def mock_tts_cache_dir_autouse(mock_tts_cache_dir): +def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> Path: """Mock the TTS cache dir with empty dir.""" return mock_tts_cache_dir