From 002a2a94853376c489234c6effb99c477c95772d Mon Sep 17 00:00:00 2001 From: Thibaut Etienne Date: Mon, 3 Jan 2022 15:08:30 +0100 Subject: [PATCH 1/6] Be more strict with mypy --- mypy.ini | 26 ++++++++++++++++++-------- pyoverkiz/client.py | 12 ++++++------ pyoverkiz/models.py | 2 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/mypy.ini b/mypy.ini index c0cc2c06..13ea0941 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,12 +1,22 @@ [mypy] -check_untyped_defs = True -disallow_untyped_defs = True -disallow_any_generics = True -warn_no_return = True -follow_imports = normal -ignore_missing_imports = True -namespace_packages = True -show_error_codes = True +python_version = 3.8 +show_error_codes = true +follow_imports = silent +ignore_missing_imports = true +strict_equality = true +warn_incomplete_stub = true +warn_redundant_casts = true +warn_unused_configs = true +warn_unused_ignores = true +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +#disallow_untyped_decorators = true +disallow_untyped_defs = true +no_implicit_optional = true +warn_return_any = true +warn_unreachable = true [mypy-tests.*] ignore_errors = True diff --git a/pyoverkiz/client.py b/pyoverkiz/client.py index de882a6f..e8df1c99 100644 --- a/pyoverkiz/client.py +++ b/pyoverkiz/client.py @@ -5,7 +5,7 @@ import urllib.parse from json import JSONDecodeError from types import TracebackType -from typing import Any, Dict, List, Union +from typing import Any, Dict, List, Union, cast import backoff import boto3 @@ -73,7 +73,7 @@ def __init__( username: str, password: str, server: OverkizServer, - session: ClientSession = None, + session: ClientSession | None = None, ) -> None: """ Constructor @@ -229,7 +229,7 @@ async def nexity_login(self) -> str: if "token" not in token: raise NexityServiceException("No Nexity SSO token provided.") - return token["token"] + return cast(str, token["token"]) @backoff.on_exception( backoff.expo, @@ -377,7 +377,7 @@ async def register_event_listener(self) -> str: API on a regular basis. """ response = await self.__post("events/register") - listener_id = response.get("id") + listener_id = cast(str, response.get("id")) self.event_listener_id = listener_id return listener_id @@ -465,7 +465,7 @@ async def execute_commands( "actions": [{"deviceURL": device_url, "commands": commands}], } response = await self.__post("exec/apply", payload) - return response["execId"] + return cast(str, response["execId"]) @backoff.on_exception( backoff.expo, @@ -496,7 +496,7 @@ async def get_places(self) -> Place: async def execute_scenario(self, oid: str) -> str: """Execute a scenario""" response = await self.__post(f"exec/{oid}") - return response["execId"] + return cast(str, response["execId"]) async def __get(self, path: str) -> Any: """Make a GET request to the OverKiz API""" diff --git a/pyoverkiz/models.py b/pyoverkiz/models.py index 63891a31..3f4270e7 100644 --- a/pyoverkiz/models.py +++ b/pyoverkiz/models.py @@ -307,7 +307,7 @@ def __len__(self) -> int: get = __getitem__ -class Command(dict): # type: ignore +class Command(dict): """Represents an OverKiz Command.""" def __init__(self, name: str, parameters: list[str] | None = None, **_: Any): From 9fefbc326ca3e13b1ffe56fc832edc37a2a3551d Mon Sep 17 00:00:00 2001 From: Thibaut Etienne Date: Mon, 3 Jan 2022 15:13:25 +0100 Subject: [PATCH 2/6] Update State definition --- pyoverkiz/models.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pyoverkiz/models.py b/pyoverkiz/models.py index 3f4270e7..3eb66e51 100644 --- a/pyoverkiz/models.py +++ b/pyoverkiz/models.py @@ -269,7 +269,13 @@ class State: type: DataType value: str | None = None - def __init__(self, name: str, type: int, value: str | None = None, **_: Any): + def __init__( + self, + name: str, + type: int, + value: None | int | float | str | bool = None, + **_: Any, + ): self.name = name self.value = value self.type = DataType(type) From 99c6079924b5fee9b31926269a36f5079f860589 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Wed, 5 Jan 2022 13:50:50 +0100 Subject: [PATCH 3/6] fix one mypy error --- pyoverkiz/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyoverkiz/models.py b/pyoverkiz/models.py index 3eb66e51..c15e98f2 100644 --- a/pyoverkiz/models.py +++ b/pyoverkiz/models.py @@ -267,7 +267,7 @@ def __len__(self) -> int: class State: name: str type: DataType - value: str | None = None + value: None | int | float | str | bool def __init__( self, From 92edd070d0c40b70da4103e6bad26749757dabb5 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Wed, 5 Jan 2022 14:11:46 +0100 Subject: [PATCH 4/6] Remove (hopefully) unneccessary Lambda --- pyoverkiz/client.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pyoverkiz/client.py b/pyoverkiz/client.py index e8df1c99..f1b05d56 100644 --- a/pyoverkiz/client.py +++ b/pyoverkiz/client.py @@ -196,11 +196,8 @@ async def nexity_login(self) -> str: loop = asyncio.get_event_loop() # Request access token - client = await loop.run_in_executor( - None, - lambda: boto3.client( - "cognito-idp", config=Config(region_name=NEXITY_COGNITO_REGION) - ), + client = boto3.client( + "cognito-idp", config=Config(region_name=NEXITY_COGNITO_REGION) ) aws = WarrantLite( From 93cca3abb529694d37b8a48d879fcb57c5432d32 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Wed, 5 Jan 2022 14:26:35 +0100 Subject: [PATCH 5/6] Fix mypy issues --- pyoverkiz/client.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pyoverkiz/client.py b/pyoverkiz/client.py index f1b05d56..2e3bd3bb 100644 --- a/pyoverkiz/client.py +++ b/pyoverkiz/client.py @@ -199,7 +199,6 @@ async def nexity_login(self) -> str: client = boto3.client( "cognito-idp", config=Config(region_name=NEXITY_COGNITO_REGION) ) - aws = WarrantLite( username=self.username, password=self.password, @@ -334,7 +333,7 @@ async def get_device_definition(self, deviceurl: str) -> JSON | None: """ Retrieve a particular setup device definition """ - response = await self.__get( + response: dict = await self.__get( f"setup/devices/{urllib.parse.quote_plus(deviceurl)}" ) @@ -438,7 +437,10 @@ async def execute_command( """Send a command""" if isinstance(command, str): command = Command(command) - return await self.execute_commands(device_url, [command], label) + + response: str = await self.execute_commands(device_url, [command], label) + + return response @backoff.on_exception( backoff.expo, NotAuthenticatedException, max_tries=2, on_backoff=relogin @@ -461,7 +463,7 @@ async def execute_commands( "label": label, "actions": [{"deviceURL": device_url, "commands": commands}], } - response = await self.__post("exec/apply", payload) + response: dict = await self.__post("exec/apply", payload) return cast(str, response["execId"]) @backoff.on_exception( From 97ac5a6d6580e9d27ad554c34c91432191617360 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Wed, 5 Jan 2022 14:38:29 +0100 Subject: [PATCH 6/6] Improve mypy typing --- pyoverkiz/client.py | 9 +++++++++ pyoverkiz/const.py | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pyoverkiz/client.py b/pyoverkiz/client.py index 2e3bd3bb..d0aabea6 100644 --- a/pyoverkiz/client.py +++ b/pyoverkiz/client.py @@ -68,6 +68,15 @@ async def refresh_listener(invocation: dict[str, Any]) -> None: class OverkizClient: """Interface class for the Overkiz API""" + username: str + password: str + server: OverkizServer + setup: Setup | None + devices: list[Device] + gateways: list[Gateway] + event_listener_id: str | None + session: ClientSession + def __init__( self, username: str, diff --git a/pyoverkiz/const.py b/pyoverkiz/const.py index 087864a2..ca4274e2 100644 --- a/pyoverkiz/const.py +++ b/pyoverkiz/const.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from pyoverkiz.models import OverkizServer COZYTOUCH_ATLANTIC_API = "https://api.groupe-atlantic.com" @@ -10,7 +12,7 @@ NEXITY_COGNITO_USER_POOL = "eu-west-1_wj277ucoI" NEXITY_COGNITO_REGION = "eu-west-1" -SUPPORTED_SERVERS = { +SUPPORTED_SERVERS: dict[str, OverkizServer] = { "atlantic_cozytouch": OverkizServer( name="Atlantic Cozytouch", endpoint="https://ha110-1.overkiz.com/enduser-mobile-web/enduserAPI/",