Add type hints

Related #2273
This commit is contained in:
MattHag 2024-11-05 00:24:50 +01:00 committed by Peter F. Patel-Schneider
parent 4c160d1723
commit 5a9725ee17
4 changed files with 9 additions and 13 deletions

View File

@ -649,7 +649,6 @@ class Battery:
elif isinstance(self.level, int): elif isinstance(self.level, int):
status = self.status.name.lower().replace("_", " ") if self.status is not None else "Unknown" status = self.status.name.lower().replace("_", " ") if self.status is not None else "Unknown"
return _("Battery: %(percent)d%% (%(status)s)") % {"percent": self.level, "status": _(status)} return _("Battery: %(percent)d%% (%(status)s)") % {"percent": self.level, "status": _(status)}
else:
return "" return ""

View File

@ -20,6 +20,7 @@ import struct
import time import time
from enum import IntEnum from enum import IntEnum
from typing import Any
from solaar.i18n import _ from solaar.i18n import _
@ -276,7 +277,7 @@ class Settings(Setting):
self._value[int(key)] = value self._value[int(key)] = value
self._pre_write(save) self._pre_write(save)
def write_key_value(self, key, value, save=True): def write_key_value(self, key, value, save=True) -> Any | None:
assert hasattr(self, "_value") assert hasattr(self, "_value")
assert hasattr(self, "_device") assert hasattr(self, "_device")
assert key is not None assert key is not None

View File

@ -33,7 +33,6 @@ from . import common
from . import descriptors from . import descriptors
from . import desktop_notifications from . import desktop_notifications
from . import diversion from . import diversion
from . import hidpp10_constants
from . import hidpp20 from . import hidpp20
from . import hidpp20_constants from . import hidpp20_constants
from . import settings from . import settings
@ -46,7 +45,6 @@ from .hidpp20_constants import ParamId
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
_hidpp20 = hidpp20.Hidpp20() _hidpp20 = hidpp20.Hidpp20()
_DK = hidpp10_constants.DEVICE_KIND
_F = hidpp20_constants.SupportedFeature _F = hidpp20_constants.SupportedFeature
@ -1612,8 +1610,6 @@ class LEDZoneSetting(settings.Setting):
period_field = {"name": _LEDP.period, "kind": settings.Kind.RANGE, "label": _("Period"), "min": 100, "max": 5000} period_field = {"name": _LEDP.period, "kind": settings.Kind.RANGE, "label": _("Period"), "min": 100, "max": 5000}
intensity_field = {"name": _LEDP.intensity, "kind": settings.Kind.RANGE, "label": _("Intensity"), "min": 0, "max": 100} intensity_field = {"name": _LEDP.intensity, "kind": settings.Kind.RANGE, "label": _("Intensity"), "min": 0, "max": 100}
ramp_field = {"name": _LEDP.ramp, "kind": settings.Kind.CHOICE, "label": _("Ramp"), "choices": hidpp20.LEDRampChoices} ramp_field = {"name": _LEDP.ramp, "kind": settings.Kind.CHOICE, "label": _("Ramp"), "choices": hidpp20.LEDRampChoices}
# form_field = {"name": _LEDP.form, "kind": settings.Kind.CHOICE, "label": _("Form"), "choices":
# _hidpp20.LEDFormChoices}
possible_fields = [color_field, speed_field, period_field, intensity_field, ramp_field] possible_fields = [color_field, speed_field, period_field, intensity_field, ramp_field]
@classmethod @classmethod
@ -1948,7 +1944,7 @@ def check_feature_settings(device, already_known) -> bool:
return True return True
def check_feature_setting(device, setting_name) -> settings.Setting | None: def check_feature_setting(device, setting_name: str) -> settings.Setting | None:
for sclass in SETTINGS: for sclass in SETTINGS:
if sclass.feature and sclass.name == setting_name and device.features: if sclass.feature and sclass.name == setting_name and device.features:
setting = check_feature(device, sclass) setting = check_feature(device, sclass)

View File

@ -43,11 +43,11 @@ class Kind(IntEnum):
class Validator: class Validator:
@classmethod @classmethod
def build(cls, setting_class, device, **kwargs): def build(cls, setting_class, device, **kwargs) -> Validator:
return cls(**kwargs) return cls(**kwargs)
@classmethod @classmethod
def to_string(cls, value): def to_string(cls, value) -> str:
return str(value) return str(value)
def compare(self, args, current): def compare(self, args, current):
@ -200,7 +200,7 @@ class BitFieldValidator(Validator):
assert isinstance(byte_count, int) and byte_count >= self.byte_count assert isinstance(byte_count, int) and byte_count >= self.byte_count
self.byte_count = byte_count self.byte_count = byte_count
def to_string(self, value): def to_string(self, value) -> str:
def element_to_string(key, val): def element_to_string(key, val):
k = next((k for k in self.options if int(key) == k), None) k = next((k for k in self.options if int(key) == k), None)
return str(k) + ":" + str(val) if k is not None else "?" return str(k) + ":" + str(val) if k is not None else "?"
@ -381,7 +381,7 @@ class ChoicesValidator(Validator):
assert self._byte_count + self._read_skip_byte_count <= 14 assert self._byte_count + self._read_skip_byte_count <= 14
assert self._byte_count + len(self._write_prefix_bytes) <= 14 assert self._byte_count + len(self._write_prefix_bytes) <= 14
def to_string(self, value): def to_string(self, value) -> str:
return str(self.choices[value]) if isinstance(value, int) else str(value) return str(self.choices[value]) if isinstance(value, int) else str(value)
def validate_read(self, reply_bytes): def validate_read(self, reply_bytes):
@ -465,7 +465,7 @@ class ChoicesMapValidator(ChoicesValidator):
assert self._byte_count + self._read_skip_byte_count + self._key_byte_count <= 14 assert self._byte_count + self._read_skip_byte_count + self._key_byte_count <= 14
assert self._byte_count + len(self._write_prefix_bytes) + self._key_byte_count <= 14 assert self._byte_count + len(self._write_prefix_bytes) + self._key_byte_count <= 14
def to_string(self, value): def to_string(self, value) -> str:
def element_to_string(key, val): def element_to_string(key, val):
k, c = next(((k, c) for k, c in self.choices.items() if int(key) == k), (None, None)) k, c = next(((k, c) for k, c in self.choices.items() if int(key) == k), (None, None))
return str(k) + ":" + str(c[val]) if k is not None else "?" return str(k) + ":" + str(c[val]) if k is not None else "?"