From a5dc4cb1c704a21a2ed112fe952969e13a5c06e7 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 30 May 2024 21:57:09 +0200 Subject: [PATCH] Fix incorrect `zeroconf` type hint in tests (#118465) * Fix incorrect `mock_async_zeroconf` type hint * Adjust thread * One more * Fix mock_zeroconf also * Adjust * Adjust --- pylint/plugins/hass_enforce_type_hints.py | 4 ++-- tests/components/homekit/test_homekit.py | 6 +++--- tests/components/otbr/test_init.py | 4 +++- tests/components/thread/test_dataset_store.py | 10 +++++----- tests/components/thread/test_diagnostics.py | 4 ++-- tests/components/thread/test_discovery.py | 18 ++++++++++-------- tests/components/thread/test_websocket_api.py | 6 ++++-- tests/components/zeroconf/test_init.py | 16 +++++++++------- tests/conftest.py | 4 ++-- 9 files changed, 40 insertions(+), 32 deletions(-) diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 0fc522f46c2..65248ac2493 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -132,7 +132,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "issue_registry": "IssueRegistry", "legacy_auth": "LegacyApiPasswordAuthProvider", "local_auth": "HassAuthProvider", - "mock_async_zeroconf": "None", + "mock_async_zeroconf": "MagicMock", "mock_bleak_scanner_start": "MagicMock", "mock_bluetooth": "None", "mock_bluetooth_adapters": "None", @@ -140,7 +140,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "mock_get_source_ip": "None", "mock_hass_config": "None", "mock_hass_config_yaml": "None", - "mock_zeroconf": "None", + "mock_zeroconf": "MagicMock", "mqtt_client_mock": "MqttMockPahoClient", "mqtt_mock": "MqttMockHAClient", "mqtt_mock_entry": "MqttMockHAClientGenerator", diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index 77931bb74f4..55698db9b2d 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -297,7 +297,7 @@ async def test_homekit_setup( async def test_homekit_setup_ip_address( - hass: HomeAssistant, hk_driver, mock_async_zeroconf: None + hass: HomeAssistant, hk_driver, mock_async_zeroconf: MagicMock ) -> None: """Test setup with given IP address.""" entry = MockConfigEntry( @@ -344,7 +344,7 @@ async def test_homekit_setup_ip_address( async def test_homekit_with_single_advertise_ips( hass: HomeAssistant, hk_driver, - mock_async_zeroconf: None, + mock_async_zeroconf: MagicMock, hass_storage: dict[str, Any], ) -> None: """Test setup with a single advertise ips.""" @@ -379,7 +379,7 @@ async def test_homekit_with_single_advertise_ips( async def test_homekit_with_many_advertise_ips( hass: HomeAssistant, hk_driver, - mock_async_zeroconf: None, + mock_async_zeroconf: MagicMock, hass_storage: dict[str, Any], ) -> None: """Test setup with many advertise ips.""" diff --git a/tests/components/otbr/test_init.py b/tests/components/otbr/test_init.py index 323e8c02f8b..0c56e9ac8da 100644 --- a/tests/components/otbr/test_init.py +++ b/tests/components/otbr/test_init.py @@ -41,7 +41,9 @@ DATASET_NO_CHANNEL = bytes.fromhex( async def test_import_dataset( - hass: HomeAssistant, mock_async_zeroconf: None, issue_registry: ir.IssueRegistry + hass: HomeAssistant, + mock_async_zeroconf: MagicMock, + issue_registry: ir.IssueRegistry, ) -> None: """Test the active dataset is imported at setup.""" add_service_listener_called = asyncio.Event() diff --git a/tests/components/thread/test_dataset_store.py b/tests/components/thread/test_dataset_store.py index 621867ae9cd..4bec9aea011 100644 --- a/tests/components/thread/test_dataset_store.py +++ b/tests/components/thread/test_dataset_store.py @@ -2,7 +2,7 @@ import asyncio from typing import Any -from unittest.mock import ANY, AsyncMock, patch +from unittest.mock import ANY, AsyncMock, MagicMock, patch import pytest from python_otbr_api.tlv_parser import TLVError @@ -710,7 +710,7 @@ async def test_set_preferred_extended_address(hass: HomeAssistant) -> None: async def test_automatically_set_preferred_dataset( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test automatically setting the first dataset as the preferred dataset.""" add_service_listener_called = asyncio.Event() @@ -775,7 +775,7 @@ async def test_automatically_set_preferred_dataset( async def test_automatically_set_preferred_dataset_own_and_other_router( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test automatically setting the first dataset as the preferred dataset. @@ -854,7 +854,7 @@ async def test_automatically_set_preferred_dataset_own_and_other_router( async def test_automatically_set_preferred_dataset_other_router( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test automatically setting the first dataset as the preferred dataset. @@ -922,7 +922,7 @@ async def test_automatically_set_preferred_dataset_other_router( async def test_automatically_set_preferred_dataset_no_router( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test automatically setting the first dataset as the preferred dataset. diff --git a/tests/components/thread/test_diagnostics.py b/tests/components/thread/test_diagnostics.py index 15ab0750316..ce86ba3532c 100644 --- a/tests/components/thread/test_diagnostics.py +++ b/tests/components/thread/test_diagnostics.py @@ -1,7 +1,7 @@ """Test the thread websocket API.""" import dataclasses -from unittest.mock import Mock, patch +from unittest.mock import MagicMock, Mock, patch import pytest from syrupy.assertion import SnapshotAssertion @@ -182,7 +182,7 @@ def ndb() -> Mock: async def test_diagnostics( hass: HomeAssistant, - mock_async_zeroconf: None, + mock_async_zeroconf: MagicMock, ndb: Mock, hass_client: ClientSessionGenerator, snapshot: SnapshotAssertion, diff --git a/tests/components/thread/test_discovery.py b/tests/components/thread/test_discovery.py index bdfd0390b9a..d9895aa72b2 100644 --- a/tests/components/thread/test_discovery.py +++ b/tests/components/thread/test_discovery.py @@ -1,6 +1,6 @@ """Test the thread websocket API.""" -from unittest.mock import ANY, AsyncMock, Mock +from unittest.mock import ANY, AsyncMock, MagicMock, Mock import pytest from zeroconf.asyncio import AsyncServiceInfo @@ -24,7 +24,9 @@ from . import ( ) -async def test_discover_routers(hass: HomeAssistant, mock_async_zeroconf: None) -> None: +async def test_discover_routers( + hass: HomeAssistant, mock_async_zeroconf: MagicMock +) -> None: """Test discovering thread routers.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_remove_service_listener = AsyncMock() @@ -151,7 +153,7 @@ async def test_discover_routers(hass: HomeAssistant, mock_async_zeroconf: None) ], ) async def test_discover_routers_unconfigured( - hass: HomeAssistant, mock_async_zeroconf: None, data, unconfigured + hass: HomeAssistant, mock_async_zeroconf: MagicMock, data, unconfigured ) -> None: """Test discovering thread routers and setting the unconfigured flag.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() @@ -197,7 +199,7 @@ async def test_discover_routers_unconfigured( "data", [ROUTER_DISCOVERY_HASS_BAD_DATA, ROUTER_DISCOVERY_HASS_MISSING_DATA] ) async def test_discover_routers_bad_or_missing_optional_data( - hass: HomeAssistant, mock_async_zeroconf: None, data + hass: HomeAssistant, mock_async_zeroconf: MagicMock, data ) -> None: """Test discovering thread routers with bad or missing vendor mDNS data.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() @@ -247,7 +249,7 @@ async def test_discover_routers_bad_or_missing_optional_data( ], ) async def test_discover_routers_bad_or_missing_mandatory_data( - hass: HomeAssistant, mock_async_zeroconf: None, service + hass: HomeAssistant, mock_async_zeroconf: MagicMock, service ) -> None: """Test discovering thread routers with missing mandatory mDNS data.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() @@ -281,7 +283,7 @@ async def test_discover_routers_bad_or_missing_mandatory_data( async def test_discover_routers_get_service_info_fails( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test discovering thread routers with invalid mDNS data.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() @@ -311,7 +313,7 @@ async def test_discover_routers_get_service_info_fails( async def test_discover_routers_update_unchanged( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test discovering thread routers with identical mDNS data in update.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() @@ -353,7 +355,7 @@ async def test_discover_routers_update_unchanged( async def test_discover_routers_stop_twice( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test discovering thread routers stopping discovery twice.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() diff --git a/tests/components/thread/test_websocket_api.py b/tests/components/thread/test_websocket_api.py index b277dcafcf4..f3390a9d8b8 100644 --- a/tests/components/thread/test_websocket_api.py +++ b/tests/components/thread/test_websocket_api.py @@ -1,6 +1,6 @@ """Test the thread websocket API.""" -from unittest.mock import ANY, AsyncMock +from unittest.mock import ANY, AsyncMock, MagicMock from zeroconf.asyncio import AsyncServiceInfo @@ -315,7 +315,9 @@ async def test_set_preferred_dataset_wrong_id( async def test_discover_routers( - hass: HomeAssistant, hass_ws_client: WebSocketGenerator, mock_async_zeroconf: None + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + mock_async_zeroconf: MagicMock, ) -> None: """Test discovering thread routers.""" mock_async_zeroconf.async_add_service_listener = AsyncMock() diff --git a/tests/components/zeroconf/test_init.py b/tests/components/zeroconf/test_init.py index 6a21212ed6e..a0b2d546dec 100644 --- a/tests/components/zeroconf/test_init.py +++ b/tests/components/zeroconf/test_init.py @@ -1,7 +1,7 @@ """Test Zeroconf component setup process.""" from typing import Any -from unittest.mock import call, patch +from unittest.mock import MagicMock, call, patch import pytest from zeroconf import ( @@ -148,7 +148,7 @@ def get_zeroconf_info_mock_model(model): return mock_zc_info -async def test_setup(hass: HomeAssistant, mock_async_zeroconf: None) -> None: +async def test_setup(hass: HomeAssistant, mock_async_zeroconf: MagicMock) -> None: """Test configured options for a device are loaded via config entry.""" mock_zc = { "_http._tcp.local.": [ @@ -238,7 +238,7 @@ async def test_setup_with_overly_long_url_and_name( async def test_setup_with_defaults( - hass: HomeAssistant, mock_zeroconf: None, mock_async_zeroconf: None + hass: HomeAssistant, mock_zeroconf: MagicMock, mock_async_zeroconf: None ) -> None: """Test default interface config.""" with ( @@ -994,7 +994,9 @@ async def test_info_from_service_can_return_ipv6(hass: HomeAssistant) -> None: assert info.host == "fd11:1111:1111:0:1234:1234:1234:1234" -async def test_get_instance(hass: HomeAssistant, mock_async_zeroconf: None) -> None: +async def test_get_instance( + hass: HomeAssistant, mock_async_zeroconf: MagicMock +) -> None: """Test we get an instance.""" assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}}) assert await zeroconf.async_get_async_instance(hass) is mock_async_zeroconf @@ -1285,7 +1287,7 @@ async def test_async_detect_interfaces_explicitly_set_ipv6_freebsd( ) -async def test_no_name(hass: HomeAssistant, mock_async_zeroconf: None) -> None: +async def test_no_name(hass: HomeAssistant, mock_async_zeroconf: MagicMock) -> None: """Test fallback to Home for mDNS announcement if the name is missing.""" hass.config.location_name = "" with patch("homeassistant.components.zeroconf.HaZeroconf"): @@ -1299,7 +1301,7 @@ async def test_no_name(hass: HomeAssistant, mock_async_zeroconf: None) -> None: async def test_setup_with_disallowed_characters_in_local_name( - hass: HomeAssistant, mock_async_zeroconf: None, caplog: pytest.LogCaptureFixture + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test we still setup with disallowed characters in the location name.""" with ( @@ -1323,7 +1325,7 @@ async def test_setup_with_disallowed_characters_in_local_name( async def test_start_with_frontend( - hass: HomeAssistant, mock_async_zeroconf: None + hass: HomeAssistant, mock_async_zeroconf: MagicMock ) -> None: """Test we start with the frontend.""" with patch("homeassistant.components.zeroconf.HaZeroconf"): diff --git a/tests/conftest.py b/tests/conftest.py index 5d992297855..4a33ea0e482 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1190,7 +1190,7 @@ def disable_translations_once(translations_once): @pytest.fixture -def mock_zeroconf() -> Generator[None, None, None]: +def mock_zeroconf() -> Generator[MagicMock, None, None]: """Mock zeroconf.""" from zeroconf import DNSCache # pylint: disable=import-outside-toplevel @@ -1206,7 +1206,7 @@ def mock_zeroconf() -> Generator[None, None, None]: @pytest.fixture -def mock_async_zeroconf(mock_zeroconf: None) -> Generator[None, None, None]: +def mock_async_zeroconf(mock_zeroconf: MagicMock) -> Generator[MagicMock, None, None]: """Mock AsyncZeroconf.""" from zeroconf import DNSCache, Zeroconf # pylint: disable=import-outside-toplevel from zeroconf.asyncio import ( # pylint: disable=import-outside-toplevel