Skip to content

Commit 5f6b904

Browse files
committed
typing: modernize annotations
1 parent 7480d8e commit 5f6b904

File tree

19 files changed

+67
-68
lines changed

19 files changed

+67
-68
lines changed

src/enapter/http/api/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Self
2+
13
import httpx
24

35
from enapter.http.api import devices
@@ -17,7 +19,7 @@ def _new_client(self) -> httpx.AsyncClient:
1719
base_url=self._config.base_url,
1820
)
1921

20-
async def __aenter__(self) -> "Client":
22+
async def __aenter__(self) -> Self:
2123
await self._client.__aenter__()
2224
return self
2325

src/enapter/http/api/config.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import os
2-
from typing import MutableMapping, Optional
2+
from typing import MutableMapping, Self
33

44

55
class Config:
66

77
@classmethod
88
def from_env(
9-
cls,
10-
env: MutableMapping[str, str] = os.environ,
11-
namespace: str = "ENAPTER_",
12-
) -> "Config":
9+
cls, env: MutableMapping[str, str] = os.environ, namespace: str = "ENAPTER_"
10+
) -> Self:
1311
prefix = namespace + "HTTP_API_"
1412
return cls(
1513
token=env[prefix + "TOKEN"],
1614
base_url=env.get(prefix + "BASE_URL"),
1715
)
1816

19-
def __init__(self, token: str, base_url: Optional[str] = None) -> None:
17+
def __init__(self, token: str, base_url: str | None = None) -> None:
2018
if not token:
2119
raise ValueError("token is missing")
2220
self.token = token

src/enapter/http/api/devices/communication_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dataclasses
2-
from typing import Any, Dict
2+
from typing import Any, Self
33

44
from .mqtt_credentials import MQTTCredentials
55
from .mqtt_protocol import MQTTProtocol
@@ -21,7 +21,7 @@ class CommunicationConfig:
2121
channel_id: str
2222

2323
@classmethod
24-
def from_dto(cls, dto: Dict[str, Any]) -> "CommunicationConfig":
24+
def from_dto(cls, dto: dict[str, Any]) -> Self:
2525
mqtt_protocol = MQTTProtocol(dto["mqtt_protocol"])
2626
mqtt_credentials: MQTTCredentials | MQTTSCredentials | None = None
2727
match mqtt_protocol:

src/enapter/http/api/devices/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22
import datetime
3-
from typing import Any, Dict
3+
from typing import Any, Self
44

55
from .authorized_role import AuthorizedRole
66
from .device_type import DeviceType
@@ -19,7 +19,7 @@ class Device:
1919
authorized_role: AuthorizedRole
2020

2121
@classmethod
22-
def from_dto(cls, dto: Dict[str, Any]) -> "Device":
22+
def from_dto(cls, dto: dict[str, Any]) -> Self:
2323
return cls(
2424
id=dto["id"],
2525
blueprint_id=dto["blueprint_id"],
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dataclasses
2-
from typing import Any, Dict
2+
from typing import Any, Self
33

44

55
@dataclasses.dataclass
@@ -9,5 +9,5 @@ class MQTTCredentials:
99
password: str
1010

1111
@classmethod
12-
def from_dto(cls, dto: Dict[str, Any]) -> "MQTTCredentials":
12+
def from_dto(cls, dto: dict[str, Any]) -> Self:
1313
return cls(username=dto["username"], password=dto["password"])

src/enapter/http/api/devices/mqtts_credentials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dataclasses
2-
from typing import Any, Dict
2+
from typing import Any, Self
33

44

55
@dataclasses.dataclass
@@ -10,7 +10,7 @@ class MQTTSCredentials:
1010
ca_chain: str
1111

1212
@classmethod
13-
def from_dto(cls, dto: Dict[str, Any]) -> "MQTTSCredentials":
13+
def from_dto(cls, dto: dict[str, Any]) -> Self:
1414
return cls(
1515
private_key=dto["private_key"],
1616
certificate=dto["certificate"],

src/enapter/log/json_formatter.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import datetime
22
import logging
3-
from typing import Any, Dict
3+
from typing import Any
44

55
import json_log_formatter # type: ignore
66

77

88
class JSONFormatter(json_log_formatter.JSONFormatter):
9+
910
def json_record(
10-
self,
11-
message: str,
12-
extra: Dict[str, Any],
13-
record: logging.LogRecord,
14-
) -> Dict[str, Any]:
11+
self, message: str, extra: dict[str, Any], record: logging.LogRecord
12+
) -> dict[str, Any]:
1513
try:
1614
del extra["taskName"]
1715
except KeyError:
@@ -34,5 +32,5 @@ def json_record(
3432

3533
return json_record
3634

37-
def mutate_json_record(self, json_record: Dict[str, Any]) -> Dict[str, Any]:
35+
def mutate_json_record(self, json_record: dict[str, Any]) -> dict[str, Any]:
3836
return json_record

src/enapter/mdns/resolver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
class Resolver:
9+
910
def __init__(self) -> None:
1011
self._logger = LOGGER
1112
self._dns_resolver = self._new_dns_resolver()

src/enapter/mqtt/api/command_request.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dataclasses
2-
from typing import Any, Dict
2+
from typing import Any, Self
33

44
from .command_response import CommandResponse
55
from .command_state import CommandState
@@ -11,25 +11,25 @@ class CommandRequest(Message):
1111

1212
id: str
1313
name: str
14-
arguments: Dict[str, Any]
14+
arguments: dict[str, Any]
1515

1616
@classmethod
17-
def from_dto(cls, dto: Dict[str, Any]) -> "CommandRequest":
17+
def from_dto(cls, dto: dict[str, Any]) -> Self:
1818
return cls(
1919
id=dto["id"],
2020
name=dto["name"],
2121
arguments=dto.get("arguments", {}),
2222
)
2323

24-
def to_dto(self) -> Dict[str, Any]:
24+
def to_dto(self) -> dict[str, Any]:
2525
return {
2626
"id": self.id,
2727
"name": self.name,
2828
"arguments": self.arguments,
2929
}
3030

3131
def new_response(
32-
self, state: CommandState, payload: Dict[str, Any]
32+
self, state: CommandState, payload: dict[str, Any]
3333
) -> CommandResponse:
3434
return CommandResponse(
3535
id=self.id,

src/enapter/mqtt/api/command_response.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import dataclasses
2-
from typing import Any, Dict
2+
from typing import Any, Self
33

44
from .command_state import CommandState
55
from .message import Message
@@ -10,17 +10,17 @@ class CommandResponse(Message):
1010

1111
id: str
1212
state: CommandState
13-
payload: Dict[str, Any]
13+
payload: dict[str, Any]
1414

1515
@classmethod
16-
def from_dto(cls, dto: Dict[str, Any]) -> "CommandResponse":
16+
def from_dto(cls, dto: dict[str, Any]) -> Self:
1717
return cls(
1818
id=dto["id"],
1919
state=CommandState(dto["state"]),
2020
payload=dto.get("payload", {}),
2121
)
2222

23-
def to_dto(self) -> Dict[str, Any]:
23+
def to_dto(self) -> dict[str, Any]:
2424
return {
2525
"id": self.id,
2626
"state": self.state.value,

0 commit comments

Comments
 (0)