Skip to content

Commit 88132bc

Browse files
committed
chore: Use typing.Self for class-referencing type hints and dynamic instantiation
1 parent 36a6b15 commit 88132bc

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

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/diagnostics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from collections import Counter
1414
from collections.abc import Generator, Mapping
1515
from contextlib import contextmanager
16-
from typing import Any, TypeVar, cast
16+
from typing import Any, Self, TypeVar, cast
1717

1818

1919
class Diagnostics:
@@ -26,7 +26,7 @@ class Diagnostics:
2626
def __init__(self) -> None:
2727
"""Initialize Diagnostics."""
2828
self._counter: Counter = Counter()
29-
self._subkeys: dict[str, Diagnostics] = {}
29+
self._subkeys: dict[str, Self] = {}
3030

3131
def increment(self, key: str, count: int = 1) -> None:
3232
"""Increment a counter for the specified key/event."""
@@ -47,7 +47,7 @@ def as_dict(self) -> Mapping[str, Any]:
4747
data[k] = v
4848
return data
4949

50-
def subkey(self, key: str) -> "Diagnostics":
50+
def subkey(self, key: str) -> Self:
5151
"""Return sub-Diagnostics object with the specified subkey.
5252
5353
This will create a new Diagnostics object if one does not already exist
@@ -61,7 +61,7 @@ def subkey(self, key: str) -> "Diagnostics":
6161
The Diagnostics object for the specified subkey.
6262
"""
6363
if key not in self._subkeys:
64-
self._subkeys[key] = Diagnostics()
64+
self._subkeys[key] = type(self)()
6565
return self._subkeys[key]
6666

6767
@contextmanager

0 commit comments

Comments
 (0)