Skip to content

Commit 155b877

Browse files
authored
Remove some mypy excludes (#59)
* 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
1 parent a77caee commit 155b877

28 files changed

+212
-478
lines changed

pyproject.toml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,13 @@ install_types = true
6363
non_interactive = true
6464

6565
disable_error_code = [
66-
"arg-type",
67-
"assignment",
66+
"no-untyped-def",
6867
"attr-defined",
69-
"call-arg",
70-
"dict-item",
71-
"index",
72-
"misc",
7368
"no-any-return",
7469
"no-untyped-call",
75-
"no-untyped-def",
76-
"override",
77-
"return-value",
70+
"assignment",
71+
"arg-type",
7872
"union-attr",
79-
"var-annotated",
8073
]
8174

8275
[[tool.mypy.overrides]]

tests/conftest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test configuration for the ZHA component."""
22

33
import asyncio
4-
from collections.abc import Callable, Generator
4+
from collections.abc import Awaitable, Callable, Generator
55
from contextlib import contextmanager
66
import itertools
77
import logging
@@ -44,7 +44,7 @@
4444
FIXTURE_GRP_ID = 0x1001
4545
FIXTURE_GRP_NAME = "fixture group"
4646
COUNTER_NAMES = ["counter_1", "counter_2", "counter_3"]
47-
INSTANCES = []
47+
INSTANCES: list[Gateway] = []
4848
_LOGGER = logging.getLogger(__name__)
4949

5050

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

387387
async def _zha_device(zigpy_dev: zigpy.device.Device) -> Device:
388388
await zha_gateway.async_device_initialized(zigpy_dev)
389389
await zha_gateway.async_block_till_done()
390-
return zha_gateway.get_device(zigpy_dev.ieee)
390+
391+
device = zha_gateway.get_device(zigpy_dev.ieee)
392+
assert device is not None
393+
return device
391394

392395
return _zha_device
393396

tests/test_async_.py

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -378,53 +378,6 @@ def job():
378378
assert len(zha_gateway.async_add_zha_job.mock_calls) == 1
379379

380380

381-
async def test_pending_scheduler(zha_gateway: Gateway) -> None:
382-
"""Add a coro to pending tasks."""
383-
call_count = []
384-
385-
async def test_coro():
386-
"""Test Coro."""
387-
call_count.append("call")
388-
389-
for _ in range(3):
390-
zha_gateway.async_add_job(test_coro())
391-
392-
await asyncio.wait(zha_gateway._tracked_completable_tasks)
393-
394-
assert len(zha_gateway._tracked_completable_tasks) == 0
395-
assert len(call_count) == 3
396-
397-
398-
def test_add_job_pending_tasks_coro(zha_gateway: Gateway) -> None:
399-
"""Add a coro to pending tasks."""
400-
401-
async def test_coro():
402-
"""Test Coro."""
403-
404-
for _ in range(2):
405-
zha_gateway.add_job(test_coro())
406-
407-
# Ensure add_job does not run immediately
408-
assert len(zha_gateway._tracked_completable_tasks) == 0
409-
410-
411-
async def test_async_add_job_pending_tasks_coro(zha_gateway: Gateway) -> None:
412-
"""Add a coro to pending tasks."""
413-
call_count = []
414-
415-
async def test_coro():
416-
"""Test Coro."""
417-
call_count.append("call")
418-
419-
for _ in range(2):
420-
zha_gateway.async_add_job(test_coro())
421-
422-
assert len(zha_gateway._tracked_completable_tasks) == 2
423-
await zha_gateway.async_block_till_done()
424-
assert len(call_count) == 2
425-
assert len(zha_gateway._tracked_completable_tasks) == 0
426-
427-
428381
async def test_async_create_task_pending_tasks_coro(zha_gateway: Gateway) -> None:
429382
"""Add a coro to pending tasks."""
430383
call_count = []
@@ -442,78 +395,6 @@ async def test_coro():
442395
assert len(zha_gateway._tracked_completable_tasks) == 0
443396

444397

445-
async def test_async_add_job_pending_tasks_executor(zha_gateway: Gateway) -> None:
446-
"""Run an executor in pending tasks."""
447-
call_count = []
448-
449-
def test_executor():
450-
"""Test executor."""
451-
call_count.append("call")
452-
453-
async def wait_finish_callback():
454-
"""Wait until all stuff is scheduled."""
455-
await asyncio.sleep(0)
456-
await asyncio.sleep(0)
457-
458-
for _ in range(2):
459-
zha_gateway.async_add_job(test_executor)
460-
461-
await wait_finish_callback()
462-
463-
await zha_gateway.async_block_till_done()
464-
assert len(call_count) == 2
465-
466-
467-
async def test_async_add_job_pending_tasks_callback(zha_gateway: Gateway) -> None:
468-
"""Run a callback in pending tasks."""
469-
call_count = []
470-
471-
@callback
472-
def test_callback():
473-
"""Test callback."""
474-
call_count.append("call")
475-
476-
async def wait_finish_callback():
477-
"""Wait until all stuff is scheduled."""
478-
await asyncio.sleep(0)
479-
await asyncio.sleep(0)
480-
481-
for _ in range(2):
482-
zha_gateway.async_add_job(test_callback)
483-
484-
await wait_finish_callback()
485-
486-
await zha_gateway.async_block_till_done()
487-
488-
assert len(zha_gateway._tracked_completable_tasks) == 0
489-
assert len(call_count) == 2
490-
491-
492-
async def test_add_job_with_none(zha_gateway: Gateway) -> None:
493-
"""Try to add a job with None as function."""
494-
with pytest.raises(ValueError):
495-
zha_gateway.async_add_job(None, "test_arg")
496-
497-
with pytest.raises(ValueError):
498-
zha_gateway.add_job(None, "test_arg")
499-
500-
501-
async def test_async_functions_with_callback(zha_gateway: Gateway) -> None:
502-
"""Test we deal with async functions accidentally marked as callback."""
503-
runs = []
504-
505-
@callback
506-
async def test():
507-
runs.append(True)
508-
509-
await zha_gateway.async_add_job(test)
510-
assert len(runs) == 1
511-
512-
zha_gateway.async_run_job(test)
513-
await zha_gateway.async_block_till_done()
514-
assert len(runs) == 2
515-
516-
517398
async def test_async_run_job_starts_tasks_eagerly(zha_gateway: Gateway) -> None:
518399
"""Test async_run_job starts tasks eagerly."""
519400
runs = []
@@ -545,7 +426,7 @@ async def _test():
545426
@pytest.mark.parametrize("eager_start", [True, False])
546427
async def test_background_task(zha_gateway: Gateway, eager_start: bool) -> None:
547428
"""Test background tasks being quit."""
548-
result = asyncio.Future()
429+
result = asyncio.get_running_loop().create_future()
549430

550431
async def test_task():
551432
try:

tests/test_climate.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
# pylint: disable=redefined-outer-name,too-many-lines
44

55
import asyncio
6-
from collections.abc import Awaitable, Callable
6+
from collections.abc import Awaitable, Callable, Coroutine
77
import logging
8+
from typing import Any
89
from unittest.mock import AsyncMock, MagicMock, call, patch
910

1011
import pytest
@@ -13,6 +14,7 @@
1314
import zhaquirks.tuya.ts0601_trv
1415
from zigpy.device import Device as ZigpyDevice
1516
import zigpy.profiles
17+
import zigpy.quirks
1618
import zigpy.zcl.clusters
1719
from zigpy.zcl.clusters.hvac import Thermostat
1820
import zigpy.zcl.foundation as zcl_f
@@ -192,10 +194,23 @@
192194
def device_climate_mock(
193195
zigpy_device_mock: Callable[..., ZigpyDevice],
194196
device_joined: Callable[[ZigpyDevice], Awaitable[Device]],
195-
) -> Callable[..., Device]:
197+
) -> Callable[
198+
[
199+
dict[int, dict[str, Any]],
200+
dict[str, Any] | None,
201+
str | None,
202+
type[zigpy.quirks.CustomDevice] | None,
203+
],
204+
Coroutine[Any, Any, Device],
205+
]:
196206
"""Test regular thermostat device."""
197207

198-
async def _dev(clusters, plug=None, manuf=None, quirk=None):
208+
async def _dev(
209+
clusters: dict[int, dict[str, Any]],
210+
plug: dict[str, Any] | None = None,
211+
manuf: str | None = None,
212+
quirk: type[zigpy.quirks.CustomDevice] | None = None,
213+
) -> Device:
199214
plugged_attrs = ZCL_ATTR_PLUG if plug is None else {**ZCL_ATTR_PLUG, **plug}
200215
zigpy_device = zigpy_device_mock(clusters, manufacturer=manuf, quirk=quirk)
201216
zigpy_device.node_desc.mac_capability_flags |= 0b_0000_0100
@@ -544,7 +559,7 @@ async def test_hvac_mode(
544559
),
545560
)
546561
async def test_hvac_modes( # pylint: disable=unused-argument
547-
device_climate_mock: Callable[..., Device],
562+
device_climate_mock: Callable[..., Awaitable[Device]],
548563
zha_gateway: Gateway,
549564
seq_of_op,
550565
modes,
@@ -570,7 +585,7 @@ async def test_hvac_modes( # pylint: disable=unused-argument
570585
),
571586
)
572587
async def test_target_temperature(
573-
device_climate_mock: Callable[..., Device],
588+
device_climate_mock: Callable[..., Awaitable[Device]],
574589
zha_gateway: Gateway,
575590
sys_mode,
576591
preset,
@@ -609,7 +624,7 @@ async def test_target_temperature(
609624
),
610625
)
611626
async def test_target_temperature_high(
612-
device_climate_mock: Callable[..., Device],
627+
device_climate_mock: Callable[..., Awaitable[Device]],
613628
zha_gateway: Gateway,
614629
preset,
615630
unoccupied,
@@ -646,7 +661,7 @@ async def test_target_temperature_high(
646661
),
647662
)
648663
async def test_target_temperature_low(
649-
device_climate_mock: Callable[..., Device],
664+
device_climate_mock: Callable[..., Awaitable[Device]],
650665
zha_gateway: Gateway,
651666
preset,
652667
unoccupied,
@@ -848,7 +863,7 @@ async def test_set_temperature_hvac_mode(
848863

849864

850865
async def test_set_temperature_heat_cool(
851-
device_climate_mock: Callable[..., Device],
866+
device_climate_mock: Callable[..., Awaitable[Device]],
852867
zha_gateway: Gateway,
853868
):
854869
"""Test setting temperature service call in heating/cooling HVAC mode."""
@@ -911,7 +926,7 @@ async def test_set_temperature_heat_cool(
911926

912927

913928
async def test_set_temperature_heat(
914-
device_climate_mock: Callable[..., Device],
929+
device_climate_mock: Callable[..., Awaitable[Device]],
915930
zha_gateway: Gateway,
916931
):
917932
"""Test setting temperature service call in heating HVAC mode."""
@@ -971,7 +986,7 @@ async def test_set_temperature_heat(
971986

972987

973988
async def test_set_temperature_cool(
974-
device_climate_mock: Callable[..., Device],
989+
device_climate_mock: Callable[..., Awaitable[Device]],
975990
zha_gateway: Gateway,
976991
):
977992
"""Test setting temperature service call in cooling HVAC mode."""
@@ -1031,7 +1046,7 @@ async def test_set_temperature_cool(
10311046

10321047

10331048
async def test_set_temperature_wrong_mode(
1034-
device_climate_mock: Callable[..., Device],
1049+
device_climate_mock: Callable[..., Awaitable[Device]],
10351050
zha_gateway: Gateway,
10361051
):
10371052
"""Test setting temperature service call for wrong HVAC mode."""

0 commit comments

Comments
 (0)