mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Enable and convert to future annotations (#838)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import warnings
|
||||
from typing import Dict
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -37,7 +38,7 @@ def dummy_protocol():
|
||||
def credentials_hash(self) -> str:
|
||||
return "dummy hash"
|
||||
|
||||
async def send(self, request: str) -> Dict:
|
||||
async def send(self, request: str) -> dict:
|
||||
return {}
|
||||
|
||||
async def close(self) -> None:
|
||||
|
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from itertools import chain
|
||||
from typing import Dict, List, Set
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -128,10 +129,10 @@ ALL_DEVICES_SMART = (
|
||||
)
|
||||
ALL_DEVICES = ALL_DEVICES_IOT.union(ALL_DEVICES_SMART)
|
||||
|
||||
IP_MODEL_CACHE: Dict[str, str] = {}
|
||||
IP_MODEL_CACHE: dict[str, str] = {}
|
||||
|
||||
|
||||
def parametrize_combine(parametrized: List[pytest.MarkDecorator]):
|
||||
def parametrize_combine(parametrized: list[pytest.MarkDecorator]):
|
||||
"""Combine multiple pytest parametrize dev marks into one set of fixtures."""
|
||||
fixtures = set()
|
||||
for param in parametrized:
|
||||
@@ -291,7 +292,7 @@ def check_categories():
|
||||
+ hubs_smart.args[1]
|
||||
+ sensors_smart.args[1]
|
||||
)
|
||||
diffs: Set[FixtureInfo] = set(FIXTURE_DATA) - set(categorized_fixtures)
|
||||
diffs: set[FixtureInfo] = set(FIXTURE_DATA) - set(categorized_fixtures)
|
||||
if diffs:
|
||||
print(diffs)
|
||||
for diff in diffs:
|
||||
|
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from json import dumps as json_dumps
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -76,8 +77,8 @@ def discovery_mock(request, mocker):
|
||||
query_data: dict
|
||||
device_type: str
|
||||
encrypt_type: str
|
||||
login_version: Optional[int] = None
|
||||
port_override: Optional[int] = None
|
||||
login_version: int | None = None
|
||||
port_override: int | None = None
|
||||
|
||||
if "discovery_result" in fixture_data:
|
||||
discovery_data = {"result": fixture_data["discovery_result"]}
|
||||
|
@@ -1,8 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, NamedTuple, Optional, Set
|
||||
from typing import NamedTuple
|
||||
|
||||
from kasa.device_factory import _get_device_type_from_sys_info
|
||||
from kasa.device_type import DeviceType
|
||||
@@ -12,7 +14,7 @@ from kasa.smart.smartdevice import SmartDevice
|
||||
class FixtureInfo(NamedTuple):
|
||||
name: str
|
||||
protocol: str
|
||||
data: Dict
|
||||
data: dict
|
||||
|
||||
|
||||
FixtureInfo.__hash__ = lambda self: hash((self.name, self.protocol)) # type: ignore[attr-defined, method-assign]
|
||||
@@ -55,7 +57,7 @@ def idgenerator(paramtuple: FixtureInfo):
|
||||
return None
|
||||
|
||||
|
||||
def get_fixture_info() -> List[FixtureInfo]:
|
||||
def get_fixture_info() -> list[FixtureInfo]:
|
||||
"""Return raw discovery file contents as JSON. Used for discovery tests."""
|
||||
fixture_data = []
|
||||
for file, protocol in SUPPORTED_DEVICES:
|
||||
@@ -77,17 +79,17 @@ def get_fixture_info() -> List[FixtureInfo]:
|
||||
return fixture_data
|
||||
|
||||
|
||||
FIXTURE_DATA: List[FixtureInfo] = get_fixture_info()
|
||||
FIXTURE_DATA: list[FixtureInfo] = get_fixture_info()
|
||||
|
||||
|
||||
def filter_fixtures(
|
||||
desc,
|
||||
*,
|
||||
data_root_filter: Optional[str] = None,
|
||||
protocol_filter: Optional[Set[str]] = None,
|
||||
model_filter: Optional[Set[str]] = None,
|
||||
component_filter: Optional[str] = None,
|
||||
device_type_filter: Optional[List[DeviceType]] = None,
|
||||
data_root_filter: str | None = None,
|
||||
protocol_filter: set[str] | None = None,
|
||||
model_filter: set[str] | None = None,
|
||||
component_filter: str | None = None,
|
||||
device_type_filter: list[DeviceType] | None = None,
|
||||
):
|
||||
"""Filter the fixtures based on supplied parameters.
|
||||
|
||||
|
@@ -14,7 +14,11 @@ async def test_fan_speed(dev: SmartDevice, mocker: MockerFixture):
|
||||
"""Test fan speed feature."""
|
||||
fan: FanModule = dev.modules["FanModule"]
|
||||
level_feature = fan._module_features["fan_speed_level"]
|
||||
assert level_feature.minimum_value <= level_feature.value <= level_feature.maximum_value
|
||||
assert (
|
||||
level_feature.minimum_value
|
||||
<= level_feature.value
|
||||
<= level_feature.maximum_value
|
||||
)
|
||||
|
||||
call = mocker.spy(fan, "call")
|
||||
await fan.set_fan_speed_level(3)
|
||||
|
@@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
@@ -7,7 +9,7 @@ import time
|
||||
from contextlib import nullcontext as does_not_raise
|
||||
from json import dumps as json_dumps
|
||||
from json import loads as json_loads
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
@@ -335,7 +337,7 @@ class MockAesDevice:
|
||||
json = json_loads(item.decode())
|
||||
return await self._post(url, json)
|
||||
|
||||
async def _post(self, url: URL, json: Dict[str, Any]):
|
||||
async def _post(self, url: URL, json: dict[str, Any]):
|
||||
if json["method"] == "handshake":
|
||||
return await self._return_handshake_response(url, json)
|
||||
elif json["method"] == "securePassthrough":
|
||||
@@ -346,7 +348,7 @@ class MockAesDevice:
|
||||
assert str(url) == f"http://{self.host}:80/app?token={self.token}"
|
||||
return await self._return_send_response(url, json)
|
||||
|
||||
async def _return_handshake_response(self, url: URL, json: Dict[str, Any]):
|
||||
async def _return_handshake_response(self, url: URL, json: dict[str, Any]):
|
||||
start = len("-----BEGIN PUBLIC KEY-----\n")
|
||||
end = len("\n-----END PUBLIC KEY-----\n")
|
||||
client_pub_key = json["params"]["key"][start:-end]
|
||||
@@ -359,7 +361,7 @@ class MockAesDevice:
|
||||
self.status_code, {"result": {"key": key_64}, "error_code": self.error_code}
|
||||
)
|
||||
|
||||
async def _return_secure_passthrough_response(self, url: URL, json: Dict[str, Any]):
|
||||
async def _return_secure_passthrough_response(self, url: URL, json: dict[str, Any]):
|
||||
encrypted_request = json["params"]["request"]
|
||||
decrypted_request = self.encryption_session.decrypt(encrypted_request.encode())
|
||||
decrypted_request_dict = json_loads(decrypted_request)
|
||||
@@ -378,7 +380,7 @@ class MockAesDevice:
|
||||
}
|
||||
return self._mock_response(self.status_code, result)
|
||||
|
||||
async def _return_login_response(self, url: URL, json: Dict[str, Any]):
|
||||
async def _return_login_response(self, url: URL, json: dict[str, Any]):
|
||||
if "token=" in str(url):
|
||||
raise Exception("token should not be in url for a login request")
|
||||
self.token = "".join(random.choices(string.ascii_uppercase, k=32)) # noqa: S311
|
||||
@@ -386,7 +388,7 @@ class MockAesDevice:
|
||||
self.inner_call_count += 1
|
||||
return self._mock_response(self.status_code, result)
|
||||
|
||||
async def _return_send_response(self, url: URL, json: Dict[str, Any]):
|
||||
async def _return_send_response(self, url: URL, json: dict[str, Any]):
|
||||
result = {"result": {"method": None}, "error_code": self.inner_error_code}
|
||||
response = self.send_response if self.send_response else result
|
||||
self.inner_call_count += 1
|
||||
|
@@ -1,7 +1,9 @@
|
||||
"""Tests for SMART devices."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
@@ -99,7 +101,7 @@ async def test_update_module_queries(dev: SmartDevice, mocker: MockerFixture):
|
||||
assert dev.modules
|
||||
|
||||
await dev.update()
|
||||
full_query: Dict[str, Any] = {}
|
||||
full_query: dict[str, Any] = {}
|
||||
for mod in dev.modules.values():
|
||||
full_query = {**full_query, **mod.query()}
|
||||
|
||||
|
Reference in New Issue
Block a user