8000 Add type hints to feature set_value (#974) · python-kasa/python-kasa@4cf3954 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4cf3954

Browse files
authored
Add type hints to feature set_value (#974)
To prevent untyped call mypy errors in consumers
1 parent 7f24408 commit 4cf3954

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

kasa/feature.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
if TYPE_CHECKING:
1111
from .device import Device
1212

13-
1413
_LOGGER = logging.getLogger(__name__)
1514

1615

@@ -140,22 +139,24 @@ def value(self):
140139
raise ValueError("Not an action and no attribute_getter set")
141140

142141
container = self.container if self.container is not None else self.device
143-
if isinstance(self.attribute_getter, Callable):
142+
if callable(self.attribute_getter):
144143
return self.attribute_getter(container)
145144
return getattr(container, self.attribute_getter)
146145

147-
async def set_value(self, value):
146+
async def set_value(self, value: int | float | bool | str | Enum | None) -> Any:
148147
"""Set the value."""
149148
if self.attribute_setter is None:
150149
raise ValueError("Tried to set read-only feature.")
151150
if self.type == Feature.Type.Number: # noqa: SIM102
151+
if not isinstance(value, (int, float)):
152+
raise ValueError("value must be a number")
152153
if value < self.minimum_value or value > self.maximum_value:
153154
raise ValueError(
154155
f"Value {value} out of range "
155156
f"[{self.minimum_value}, {self.maximum_value}]"
156157
)
157158
elif self.type == Feature.Type.Choice: # noqa: SIM102
158-
if value not in self.choices:
159+
if not self.choices or value not in self.choices:
159160
raise ValueError(
160161
f"Unexpected value for {self.name}: {value}"
161162
f" - allowed: {self.choices}"

0 commit comments

Comments
 (0)
0