Skip to content

Commit 8551bd8

Browse files
authored
chore: align mypy config and type stubs in pyproject.toml (#1072)
1 parent ad475f7 commit 8551bd8

12 files changed

Lines changed: 63 additions & 21 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ repos:
4242
args:
4343
- --fix
4444
- repo: https://github.com/pre-commit/mirrors-mypy
45-
rev: v1.7.1
45+
rev: v1.19.1
4646
hooks:
4747
- id: mypy
48-
exclude: cli.py
49-
additional_dependencies: [ "types-paho-mqtt", "types-protobuf" ]
48+
exclude: ^roborock/cli\.py$
49+
additional_dependencies: [ "types-PyYAML", "types-paho-mqtt", "types-protobuf" ]
5050
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
5151
rev: v9.23.0
5252
hooks:

‎mypy.ini‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎pyproject.toml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ dev = [
5959
"pytest",
6060
"pre-commit>=3.5,<5.0",
6161
"mypy",
62+
"types-PyYAML",
63+
"types-paho-mqtt",
64+
"types-protobuf",
6265
"ruff==0.14.11",
6366
"codespell",
6467
"aioresponses>=0.7.7,<0.8",
@@ -143,8 +146,15 @@ extend-exclude = ["roborock/map/proto/*_pb2.py"]
143146
"roborock/map/proto/*_pb2.py" = ["E501", "I001", "UP009"]
144147
"tests/*" = ["B010"]
145148

149+
[tool.mypy]
150+
check_untyped_defs = true
151+
152+
[[tool.mypy.overrides]]
153+
module = ["construct"]
154+
ignore_missing_imports = true
155+
146156
[[tool.mypy.overrides]]
147-
module = ["roborock.map.proto.*"]
157+
module = ["roborock.cli", "roborock.map.proto.*"]
148158
ignore_errors = true
149159

150160
[tool.pytest.ini_options]

‎roborock/data/containers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def from_dict(cls, data: dict[str, Any]):
129129
return cls(**result)
130130

131131
@staticmethod
132-
def convert_dict(types_map: dict[Any, type], data: dict[Any, Any]) -> dict[Any, Any]:
132+
def convert_dict(types_map: dict[Any, Any], data: dict[Any, Any]) -> dict[Any, Any]:
133133
"""Generic helper to convert a dictionary of values based on a schema map of types.
134134
135135
This is meant to be used by traits that use dataclass reflection similar to

‎roborock/devices/traits/common.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ class DpsDataConverter(Generic[TDps]):
7777
to optimize repeated updates from device streams.
7878
"""
7979

80-
def __init__(self, dps_type_map: dict[TDps, type], dps_field_map: dict[TDps, str]):
80+
def __init__(self, dps_type_map: dict[TDps, Any], dps_field_map: dict[TDps, str]):
8181
"""Initialize the converter for a specific RoborockBase-derived class."""
8282
self._dps_type_map = dps_type_map
8383
self._dps_field_map = dps_field_map
8484

8585
@classmethod
8686
def from_dataclass(cls, dataclass_type: type[RoborockBase]):
8787
"""Initialize the converter for a specific RoborockBase-derived class."""
88-
dps_type_map: dict[TDps, type] = {}
88+
dps_type_map: dict[TDps, Any] = {}
8989
dps_field_map: dict[TDps, str] = {}
9090
for field_obj in dataclasses.fields(dataclass_type):
9191
if field_obj.metadata and "dps" in field_obj.metadata:

‎roborock/mqtt/roborock_session.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import datetime
1313
import logging
1414
import ssl
15-
from collections.abc import Callable
15+
from collections.abc import AsyncIterator, Callable
1616
from contextlib import asynccontextmanager
1717

1818
import aiomqtt
@@ -249,7 +249,7 @@ async def _run_connection(self, start_future: asyncio.Future[None] | None) -> No
249249
raise
250250

251251
@asynccontextmanager
252-
async def _mqtt_client(self, params: MqttParams) -> aiomqtt.Client:
252+
async def _mqtt_client(self, params: MqttParams) -> AsyncIterator[aiomqtt.Client]:
253253
"""Connect to the MQTT broker and listen for messages."""
254254
_LOGGER.debug("Connecting to %s:%s for %s", params.host, params.port, params.username)
255255
tls_params = None

‎roborock/protocol.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,10 @@ def _parse(self, stream, context, path):
325325
hash1 = self.checksumfield.parse_stream(stream, **context)
326326
hash2 = self.hashfunc(self.bytesfunc(context))
327327
if hash1 != hash2:
328+
read_hex = binascii.hexlify(hash1).decode() if isinstance(hash1, bytestringtype) else hash1
329+
computed_hex = binascii.hexlify(hash2).decode() if isinstance(hash2, bytestringtype) else hash2
328330
raise ChecksumError(
329-
f"wrong checksum, read {hash1 if not isinstance(hash1, bytestringtype) else binascii.hexlify(hash1)}, "
330-
f"computed {hash2 if not isinstance(hash2, bytestringtype) else binascii.hexlify(hash2)}",
331+
f"wrong checksum, read {read_hex}, computed {computed_hex}",
331332
path=path,
332333
)
333334
return hash1

‎tests/devices/traits/a01/test_init.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ async def test_dyad_api_query_values(dyad_api: DyadApi, fake_channel: FakeChanne
8080
message = fake_channel.published_messages[0]
8181
assert message.protocol == RoborockMessageProtocol.RPC_REQUEST
8282
assert message.version == b"A01"
83+
assert message.payload is not None
8384
payload_data = json.loads(unpad(message.payload, AES.block_size))
8485
assert payload_data["dps"] == {"10000": "[209, 201, 207, 214, 215, 227, 229, 230, 222, 224]"}
8586
assert "t" in payload_data
@@ -497,6 +498,7 @@ async def test_zeo_api_query_values(zeo_api: ZeoApi, fake_channel: FakeChannel):
497498
message = fake_channel.published_messages[0]
498499
assert message.protocol == RoborockMessageProtocol.RPC_REQUEST
499500
assert message.version == b"A01"
501+
assert message.payload is not None
500502
payload_data = json.loads(unpad(message.payload, AES.block_size))
501503
assert payload_data["dps"] == {"10000": "[203, 207, 226, 227, 224, 218]"}
502504
assert "t" in payload_data
@@ -568,6 +570,7 @@ async def test_dyad_api_set_value(dyad_api: DyadApi, fake_channel: FakeChannel):
568570
assert message.version == b"A01"
569571

570572
# decode the payload to verify contents
573+
assert message.payload is not None
571574
payload_data = json.loads(unpad(message.payload, AES.block_size))
572575
# A01 protocol expects values to be strings in the dps dict
573576
assert payload_data["dps"] == {"209": 1}
@@ -623,6 +626,7 @@ async def test_dyad_action_commands(
623626
await getattr(dyad_api, method)()
624627

625628
assert len(fake_channel.published_messages) == 1
629+
assert fake_channel.published_messages[0].payload is not None
626630
payload_data = json.loads(unpad(fake_channel.published_messages[0].payload, AES.block_size))
627631
assert payload_data["dps"] == expected_dps
628632

@@ -638,6 +642,7 @@ async def test_zeo_api_set_value(zeo_api: ZeoApi, fake_channel: FakeChannel):
638642
assert message.version == b"A01"
639643

640644
# decode the payload to verify contents
645+
assert message.payload is not None
641646
payload_data = json.loads(unpad(message.payload, AES.block_size))
642647
# A01 protocol expects values to be strings in the dps dict
643648
assert payload_data["dps"] == {"204": "standard"}

‎tests/fixtures/web_api_fixtures.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def home_data_fixture() -> dict[str, Any]:
2929

3030

3131
@pytest.fixture(name="mock_rest")
32-
def mock_rest_fixture(skip_rate_limit: Any, home_data: dict[str, Any]) -> aioresponses:
32+
def mock_rest_fixture(skip_rate_limit: Any, home_data: dict[str, Any]) -> Generator[aioresponses, None, None]:
3333
"""Mock all rest endpoints so they won't hit real endpoints"""
3434
with aioresponses() as mocked:
3535
# Match the base URL and allow any query params

‎tests/map/test_b01_q10_map_parser.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_packet_layers_decompose_q10_fixture() -> None:
157157

158158
living = layers.render_room(2, (255, 0, 0, 255))
159159
image = Image.open(io.BytesIO(living))
160-
opaque = sum(1 for *_rgb, alpha in image.getdata() if alpha > 0)
160+
opaque = sum(1 for alpha in image.getchannel("A").tobytes() if alpha > 0)
161161
assert opaque == next(room.pixel_count for room in layers.rooms if room.id == 2)
162162

163163

0 commit comments

Comments
 (0)