mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve contextmanager typing (#122250)
This commit is contained in:
parent
5fd3b929f4
commit
6be4ef8a1f
@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
import contextlib
|
import contextlib
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
import copy
|
import copy
|
||||||
@ -157,7 +158,7 @@ class ZhaRadioManager:
|
|||||||
return mgr
|
return mgr
|
||||||
|
|
||||||
@contextlib.asynccontextmanager
|
@contextlib.asynccontextmanager
|
||||||
async def connect_zigpy_app(self) -> ControllerApplication:
|
async def connect_zigpy_app(self) -> AsyncIterator[ControllerApplication]:
|
||||||
"""Connect to the radio with the current config and then clean up."""
|
"""Connect to the radio with the current config and then clean up."""
|
||||||
assert self.radio_type is not None
|
assert self.radio_type is not None
|
||||||
|
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
"""Test built-in blueprints."""
|
"""Test built-in blueprints."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections.abc import Iterator
|
||||||
import contextlib
|
import contextlib
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from os import PathLike
|
||||||
import pathlib
|
import pathlib
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
@ -23,7 +25,9 @@ BUILTIN_BLUEPRINT_FOLDER = pathlib.Path(automation.__file__).parent / "blueprint
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def patch_blueprint(blueprint_path: str, data_path):
|
def patch_blueprint(
|
||||||
|
blueprint_path: str, data_path: str | PathLike[str]
|
||||||
|
) -> Iterator[None]:
|
||||||
"""Patch blueprint loading from a different source."""
|
"""Patch blueprint loading from a different source."""
|
||||||
orig_load = models.DomainBlueprints._load_blueprint
|
orig_load = models.DomainBlueprints._load_blueprint
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Iterator
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ from . import _get_manager, generate_advertisement_data, generate_ble_device
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def mock_shutdown(manager: HomeAssistantBluetoothManager) -> None:
|
def mock_shutdown(manager: HomeAssistantBluetoothManager) -> Iterator[None]:
|
||||||
"""Mock shutdown of the HomeAssistantBluetoothManager."""
|
"""Mock shutdown of the HomeAssistantBluetoothManager."""
|
||||||
manager.shutdown = True
|
manager.shutdown = True
|
||||||
yield
|
yield
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Test the Home Assistant SkyConnect config flow."""
|
"""Test the Home Assistant SkyConnect config flow."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable, Iterator
|
||||||
import contextlib
|
import contextlib
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, Mock, call, patch
|
from unittest.mock import AsyncMock, Mock, call, patch
|
||||||
@ -80,7 +80,7 @@ def mock_addon_info(
|
|||||||
update_available=False,
|
update_available=False,
|
||||||
version=None,
|
version=None,
|
||||||
),
|
),
|
||||||
):
|
) -> Iterator[tuple[Mock, Mock]]:
|
||||||
"""Mock the main addon states for the config flow."""
|
"""Mock the main addon states for the config flow."""
|
||||||
mock_flasher_manager = Mock(spec_set=get_zigbee_flasher_addon_manager(hass))
|
mock_flasher_manager = Mock(spec_set=get_zigbee_flasher_addon_manager(hass))
|
||||||
mock_flasher_manager.addon_name = "Silicon Labs Flasher"
|
mock_flasher_manager.addon_name = "Silicon Labs Flasher"
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
"""Common methods used across tests for Netatmo."""
|
"""Common methods used across tests for Netatmo."""
|
||||||
|
|
||||||
|
from collections.abc import Iterator
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
import json
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
@ -109,7 +110,7 @@ async def simulate_webhook(hass: HomeAssistant, webhook_id: str, response) -> No
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def selected_platforms(platforms: list[Platform]) -> AsyncMock:
|
def selected_platforms(platforms: list[Platform]) -> Iterator[None]:
|
||||||
"""Restrict loaded platforms to list given."""
|
"""Restrict loaded platforms to list given."""
|
||||||
with (
|
with (
|
||||||
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", platforms),
|
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", platforms),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""Provide common Renault fixtures."""
|
"""Provide common Renault fixtures."""
|
||||||
|
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator, Iterator
|
||||||
import contextlib
|
import contextlib
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -200,7 +200,7 @@ def patch_fixtures_with_no_data():
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def _patch_fixtures_with_side_effect(side_effect: Any):
|
def _patch_fixtures_with_side_effect(side_effect: Any) -> Iterator[None]:
|
||||||
"""Mock fixtures."""
|
"""Mock fixtures."""
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Aiohttp test utils."""
|
"""Aiohttp test utils."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections.abc import Iterator
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import re
|
import re
|
||||||
@ -296,7 +297,7 @@ class AiohttpClientMockResponse:
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def mock_aiohttp_client():
|
def mock_aiohttp_client() -> Iterator[AiohttpClientMocker]:
|
||||||
"""Context manager to mock aiohttp client."""
|
"""Context manager to mock aiohttp client."""
|
||||||
mocker = AiohttpClientMocker()
|
mocker = AiohttpClientMocker()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user