Skip to content

Commit d762649

Browse files
allenporterCopilot
andauthored
chore: Migrate to typing.Self and remove __future__ annotations. (#798)
* chore: Migrate to `typing.Self` and remove `__future__` annotations. * chore: Use `typing.Self` for class-referencing type hints and dynamic instantiation * chore: Update roborock/roborock_message.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 44ee75e commit d762649

File tree

14 files changed

+20
-42
lines changed

14 files changed

+20
-42
lines changed

roborock/broadcast_protocol.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
import asyncio
42
import hashlib
53
import json

roborock/data/code_mappings.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
import logging
42
from collections import namedtuple
53
from enum import Enum, IntEnum, StrEnum
@@ -17,7 +15,7 @@ def name(self) -> str:
1715
return super().name.lower()
1816

1917
@classmethod
20-
def _missing_(cls: type[RoborockEnum], key) -> RoborockEnum:
18+
def _missing_(cls: type[Self], key) -> Self:
2119
if hasattr(cls, "unknown"):
2220
warning = f"Missing {cls.__name__} code: {key} - defaulting to 'unknown'"
2321
if warning not in completed_warnings:
@@ -32,23 +30,23 @@ def _missing_(cls: type[RoborockEnum], key) -> RoborockEnum:
3230
return default_value
3331

3432
@classmethod
35-
def as_dict(cls: type[RoborockEnum]):
33+
def as_dict(cls: type[Self]):
3634
return {i.name: i.value for i in cls if i.name != "missing"}
3735

3836
@classmethod
39-
def as_enum_dict(cls: type[RoborockEnum]):
37+
def as_enum_dict(cls: type[Self]):
4038
return {i.value: i for i in cls if i.name != "missing"}
4139

4240
@classmethod
43-
def values(cls: type[RoborockEnum]) -> list[int]:
41+
def values(cls: type[Self]) -> list[int]:
4442
return list(cls.as_dict().values())
4543

4644
@classmethod
47-
def keys(cls: type[RoborockEnum]) -> list[str]:
45+
def keys(cls: type[Self]) -> list[str]:
4846
return list(cls.as_dict().keys())
4947

5048
@classmethod
51-
def items(cls: type[RoborockEnum]):
49+
def items(cls: type[Self]):
5250
return cls.as_dict().items()
5351

5452

roborock/data/v1/v1_code_mappings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Self
2+
13
from ..code_mappings import RoborockEnum
24

35

@@ -91,7 +93,7 @@ class RoborockStartType(RoborockEnum):
9193

9294
class RoborockDssCodes(RoborockEnum):
9395
@classmethod
94-
def _missing_(cls: type[RoborockEnum], key) -> RoborockEnum:
96+
def _missing_(cls: type[Self], key) -> Self:
9597
# If the calculated value is not provided, then it should be viewed as okay.
9698
# As the math will sometimes result in you getting numbers that don't matter.
9799
return cls.okay # type: ignore

roborock/device_features.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from __future__ import annotations
2-
31
from dataclasses import dataclass, field, fields
42
from enum import IntEnum, StrEnum
5-
from typing import Any
3+
from typing import Any, Self
64

75
from roborock.data.code_mappings import RoborockProductNickname
86
from roborock.data.containers import RoborockBase
@@ -566,7 +564,7 @@ def from_feature_flags(
566564
new_feature_info_str: str,
567565
feature_info: list[int],
568566
product_nickname: RoborockProductNickname | None,
569-
) -> DeviceFeatures:
567+
) -> Self:
570568
"""Creates a DeviceFeatures instance from raw feature flags.
571569
:param new_feature_info: A int from get_init_status (sometimes can be found in homedata, but it is not always)
572570
:param new_feature_info_str: A hex string from get_init_status or home_data.

roborock/devices/rpc/b01_q10_channel.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Thin wrapper around the MQTT channel for Roborock B01 Q10 devices."""
22

3-
from __future__ import annotations
4-
53
import logging
64
from collections.abc import AsyncGenerator
75
from typing import Any

roborock/devices/traits/b01/q7/clean_summary.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
and a `record_list` whose items contain a JSON string in `detail`.
55
"""
66

7-
from __future__ import annotations
8-
97
import logging
108

119
from roborock import CleanRecordDetail, CleanRecordList, CleanRecordSummary

roborock/devices/traits/v1/network_info.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Trait for device network information."""
22

3-
from __future__ import annotations
4-
53
import logging
64

75
from roborock.data import NetworkInfo

roborock/diagnostics.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
DeviceManager.
1010
"""
1111

12-
from __future__ import annotations
13-
1412
import time
1513
from collections import Counter
1614
from collections.abc import Generator, Mapping
1715
from contextlib import contextmanager
18-
from typing import Any, TypeVar, cast
16+
from typing import Any, Self, TypeVar, cast
1917

2018

2119
class Diagnostics:
@@ -28,7 +26,7 @@ class Diagnostics:
2826
def __init__(self) -> None:
2927
"""Initialize Diagnostics."""
3028
self._counter: Counter = Counter()
31-
self._subkeys: dict[str, Diagnostics] = {}
29+
self._subkeys: dict[str, Self] = {}
3230

3331
def increment(self, key: str, count: int = 1) -> None:
3432
"""Increment a counter for the specified key/event."""
@@ -49,7 +47,7 @@ def as_dict(self) -> Mapping[str, Any]:
4947
data[k] = v
5048
return data
5149

52-
def subkey(self, key: str) -> Diagnostics:
50+
def subkey(self, key: str) -> Self:
5351
"""Return sub-Diagnostics object with the specified subkey.
5452
5553
This will create a new Diagnostics object if one does not already exist
@@ -63,7 +61,7 @@ def subkey(self, key: str) -> Diagnostics:
6361
The Diagnostics object for the specified subkey.
6462
"""
6563
if key not in self._subkeys:
66-
self._subkeys[key] = Diagnostics()
64+
self._subkeys[key] = type(self)()
6765
return self._subkeys[key]
6866

6967
@contextmanager

roborock/exceptions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Roborock exceptions."""
22

3-
from __future__ import annotations
4-
53

64
class RoborockException(Exception):
75
"""Class for Roborock exceptions."""

roborock/protocol.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
import binascii
42
import gzip
53
import hashlib

0 commit comments

Comments
 (0)