Skip to content

Commit

Permalink
Remove some mypy excludes (#59)
Browse files Browse the repository at this point in the history
* Enable typing

* Commit `py.typed`

* Order disabled error codes by count

* Fix rare typing issues

* Fix `var-annotated` by dropping `attrs`

* Fix `override` by merging `PlatformEntityInfo` and `GroupEntityInfo`

* Fix typing in unit tests as well

* Drop unused job helpers

* Fix `misc`

* Revert "Commit `py.typed`"

This reverts commit 9d5d3f6.

* Revert "Enable typing"

This reverts commit 784aac3.

* Fix `call-arg` as well

* Remove counts
  • Loading branch information
puddly authored Jul 9, 2024
1 parent a77caee commit 155b877
Show file tree
Hide file tree
Showing 28 changed files with 212 additions and 478 deletions.
13 changes: 3 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,13 @@ install_types = true
non_interactive = true

disable_error_code = [
"arg-type",
"assignment",
"no-untyped-def",
"attr-defined",
"call-arg",
"dict-item",
"index",
"misc",
"no-any-return",
"no-untyped-call",
"no-untyped-def",
"override",
"return-value",
"assignment",
"arg-type",
"union-attr",
"var-annotated",
]

[[tool.mypy.overrides]]
Expand Down
11 changes: 7 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test configuration for the ZHA component."""

import asyncio
from collections.abc import Callable, Generator
from collections.abc import Awaitable, Callable, Generator
from contextlib import contextmanager
import itertools
import logging
Expand Down Expand Up @@ -44,7 +44,7 @@
FIXTURE_GRP_ID = 0x1001
FIXTURE_GRP_NAME = "fixture group"
COUNTER_NAMES = ["counter_1", "counter_2", "counter_3"]
INSTANCES = []
INSTANCES: list[Gateway] = []
_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -381,13 +381,16 @@ def globally_load_quirks():
@pytest.fixture
def device_joined(
zha_gateway: Gateway, # pylint: disable=redefined-outer-name
) -> Callable[[zigpy.device.Device], Device]:
) -> Callable[[zigpy.device.Device], Awaitable[Device]]:
"""Return a newly joined ZHAWS device."""

async def _zha_device(zigpy_dev: zigpy.device.Device) -> Device:
await zha_gateway.async_device_initialized(zigpy_dev)
await zha_gateway.async_block_till_done()
return zha_gateway.get_device(zigpy_dev.ieee)

device = zha_gateway.get_device(zigpy_dev.ieee)
assert device is not None
return device

return _zha_device

Expand Down
121 changes: 1 addition & 120 deletions tests/test_async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,53 +378,6 @@ def job():
assert len(zha_gateway.async_add_zha_job.mock_calls) == 1


async def test_pending_scheduler(zha_gateway: Gateway) -> None:
"""Add a coro to pending tasks."""
call_count = []

async def test_coro():
"""Test Coro."""
call_count.append("call")

for _ in range(3):
zha_gateway.async_add_job(test_coro())

await asyncio.wait(zha_gateway._tracked_completable_tasks)

assert len(zha_gateway._tracked_completable_tasks) == 0
assert len(call_count) == 3


def test_add_job_pending_tasks_coro(zha_gateway: Gateway) -> None:
"""Add a coro to pending tasks."""

async def test_coro():
"""Test Coro."""

for _ in range(2):
zha_gateway.add_job(test_coro())

# Ensure add_job does not run immediately
assert len(zha_gateway._tracked_completable_tasks) == 0


async def test_async_add_job_pending_tasks_coro(zha_gateway: Gateway) -> None:
"""Add a coro to pending tasks."""
call_count = []

async def test_coro():
"""Test Coro."""
call_count.append("call")

for _ in range(2):
zha_gateway.async_add_job(test_coro())

assert len(zha_gateway._tracked_completable_tasks) == 2
await zha_gateway.async_block_till_done()
assert len(call_count) == 2
assert len(zha_gateway._tracked_completable_tasks) == 0


async def test_async_create_task_pending_tasks_coro(zha_gateway: Gateway) -> None:
"""Add a coro to pending tasks."""
call_count = []
Expand All @@ -442,78 +395,6 @@ async def test_coro():
assert len(zha_gateway._tracked_completable_tasks) == 0


async def test_async_add_job_pending_tasks_executor(zha_gateway: Gateway) -> None:
"""Run an executor in pending tasks."""
call_count = []

def test_executor():
"""Test executor."""
call_count.append("call")

async def wait_finish_callback():
"""Wait until all stuff is scheduled."""
await asyncio.sleep(0)
await asyncio.sleep(0)

for _ in range(2):
zha_gateway.async_add_job(test_executor)

await wait_finish_callback()

await zha_gateway.async_block_till_done()
assert len(call_count) == 2


async def test_async_add_job_pending_tasks_callback(zha_gateway: Gateway) -> None:
"""Run a callback in pending tasks."""
call_count = []

@callback
def test_callback():
"""Test callback."""
call_count.append("call")

async def wait_finish_callback():
"""Wait until all stuff is scheduled."""
await asyncio.sleep(0)
await asyncio.sleep(0)

for _ in range(2):
zha_gateway.async_add_job(test_callback)

await wait_finish_callback()

await zha_gateway.async_block_till_done()

assert len(zha_gateway._tracked_completable_tasks) == 0
assert len(call_count) == 2


async def test_add_job_with_none(zha_gateway: Gateway) -> None:
"""Try to add a job with None as function."""
with pytest.raises(ValueError):
zha_gateway.async_add_job(None, "test_arg")

with pytest.raises(ValueError):
zha_gateway.add_job(None, "test_arg")


async def test_async_functions_with_callback(zha_gateway: Gateway) -> None:
"""Test we deal with async functions accidentally marked as callback."""
runs = []

@callback
async def test():
runs.append(True)

await zha_gateway.async_add_job(test)
assert len(runs) == 1

zha_gateway.async_run_job(test)
await zha_gateway.async_block_till_done()
assert len(runs) == 2


async def test_async_run_job_starts_tasks_eagerly(zha_gateway: Gateway) -> None:
"""Test async_run_job starts tasks eagerly."""
runs = []
Expand Down Expand Up @@ -545,7 +426,7 @@ async def _test():
@pytest.mark.parametrize("eager_start", [True, False])
async def test_background_task(zha_gateway: Gateway, eager_start: bool) -> None:
"""Test background tasks being quit."""
result = asyncio.Future()
result = asyncio.get_running_loop().create_future()

async def test_task():
try:
Expand Down
37 changes: 26 additions & 11 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# pylint: disable=redefined-outer-name,too-many-lines

import asyncio
from collections.abc import Awaitable, Callable
from collections.abc import Awaitable, Callable, Coroutine
import logging
from typing import Any
from unittest.mock import AsyncMock, MagicMock, call, patch

import pytest
Expand All @@ -13,6 +14,7 @@
import zhaquirks.tuya.ts0601_trv
from zigpy.device import Device as ZigpyDevice
import zigpy.profiles
import zigpy.quirks
import zigpy.zcl.clusters
from zigpy.zcl.clusters.hvac import Thermostat
import zigpy.zcl.foundation as zcl_f
Expand Down Expand Up @@ -192,10 +194,23 @@
def device_climate_mock(
zigpy_device_mock: Callable[..., ZigpyDevice],
device_joined: Callable[[ZigpyDevice], Awaitable[Device]],
) -> Callable[..., Device]:
) -> Callable[
[
dict[int, dict[str, Any]],
dict[str, Any] | None,
str | None,
type[zigpy.quirks.CustomDevice] | None,
],
Coroutine[Any, Any, Device],
]:
"""Test regular thermostat device."""

async def _dev(clusters, plug=None, manuf=None, quirk=None):
async def _dev(
clusters: dict[int, dict[str, Any]],
plug: dict[str, Any] | None = None,
manuf: str | None = None,
quirk: type[zigpy.quirks.CustomDevice] | None = None,
) -> Device:
plugged_attrs = ZCL_ATTR_PLUG if plug is None else {**ZCL_ATTR_PLUG, **plug}
zigpy_device = zigpy_device_mock(clusters, manufacturer=manuf, quirk=quirk)
zigpy_device.node_desc.mac_capability_flags |= 0b_0000_0100
Expand Down Expand Up @@ -544,7 +559,7 @@ async def test_hvac_mode(
),
)
async def test_hvac_modes( # pylint: disable=unused-argument
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
seq_of_op,
modes,
Expand All @@ -570,7 +585,7 @@ async def test_hvac_modes( # pylint: disable=unused-argument
),
)
async def test_target_temperature(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
sys_mode,
preset,
Expand Down Expand Up @@ -609,7 +624,7 @@ async def test_target_temperature(
),
)
async def test_target_temperature_high(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
preset,
unoccupied,
Expand Down Expand Up @@ -646,7 +661,7 @@ async def test_target_temperature_high(
),
)
async def test_target_temperature_low(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
preset,
unoccupied,
Expand Down Expand Up @@ -848,7 +863,7 @@ async def test_set_temperature_hvac_mode(


async def test_set_temperature_heat_cool(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
):
"""Test setting temperature service call in heating/cooling HVAC mode."""
Expand Down Expand Up @@ -911,7 +926,7 @@ async def test_set_temperature_heat_cool(


async def test_set_temperature_heat(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
):
"""Test setting temperature service call in heating HVAC mode."""
Expand Down Expand Up @@ -971,7 +986,7 @@ async def test_set_temperature_heat(


async def test_set_temperature_cool(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
):
"""Test setting temperature service call in cooling HVAC mode."""
Expand Down Expand Up @@ -1031,7 +1046,7 @@ async def test_set_temperature_cool(


async def test_set_temperature_wrong_mode(
device_climate_mock: Callable[..., Device],
device_climate_mock: Callable[..., Awaitable[Device]],
zha_gateway: Gateway,
):
"""Test setting temperature service call for wrong HVAC mode."""
Expand Down
Loading

0 comments on commit 155b877

Please sign in to comment.