Skip to content

Commit e3dc052

Browse files
authored
Merge pull request #20 from Enapter/rnovatorov/dev
Upgrade `aiomqtt` to 2.4
2 parents 00b21ff + f5fd485 commit e3dc052

File tree

21 files changed

+276
-191
lines changed

21 files changed

+276
-191
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ name: CI
22

33
on:
44
push:
5-
pull_request:
65

76
jobs:
87
Ubuntu:

Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@ update-deps:
1313
check: lint test
1414

1515
.PHONY: lint
16-
lint:
16+
lint: lint-black lint-isort lint-pyflakes lint-mypy
17+
18+
.PHONY: lint-black
19+
lint-black:
1720
pipenv run black --check .
21+
22+
.PHONY: lint-isort
23+
lint-isort:
1824
pipenv run isort --check .
25+
26+
.PHONY: lint-pyflakes
27+
lint-pyflakes:
1928
pipenv run pyflakes .
2029

30+
.PHONY: lint-mypy
31+
lint-mypy:
32+
pipenv run mypy enapter
33+
2134
.PHONY: test
2235
test: run-unit-tests run-integration-tests
2336

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ black = "*"
1111
docker = "*"
1212
faker = "*"
1313
isort = "*"
14+
mypy = "*"
1415
pyflakes = "*"
1516
pytest = "*"
1617
pytest-asyncio = "*"

enapter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.11.1"
1+
__version__ = "0.11.3"
22

33
from . import async_, log, mdns, mqtt, types, vucm
44

enapter/async_/generator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import contextlib
22
import functools
3+
from typing import AsyncContextManager, AsyncGenerator, Callable
34

45

5-
def generator(func):
6+
def generator(
7+
func: Callable[..., AsyncGenerator],
8+
) -> Callable[..., AsyncContextManager[AsyncGenerator]]:
69
@functools.wraps(func)
710
@contextlib.asynccontextmanager
8-
async def wrapper(*args, **kwargs):
11+
async def wrapper(*args, **kwargs) -> AsyncGenerator[AsyncGenerator, None]:
912
gen = func(*args, **kwargs)
1013
try:
1114
yield gen

enapter/async_/routine.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55

66
class Routine(abc.ABC):
77
@abc.abstractmethod
8-
async def _run(self):
8+
async def _run(self) -> None:
99
raise NotImplementedError # pragma: no cover
1010

1111
async def __aenter__(self):
1212
await self.start()
1313
return self
1414

15-
async def __aexit__(self, *_):
15+
async def __aexit__(self, *_) -> None:
1616
await self.stop()
1717

18-
def task(self):
18+
def task(self) -> asyncio.Task:
1919
return self._task
2020

21-
async def start(self, cancel_parent_task_on_exception=True):
21+
async def start(self, cancel_parent_task_on_exception: bool = True) -> None:
2222
self._started = asyncio.Event()
2323
self._stack = contextlib.AsyncExitStack()
2424

@@ -43,26 +43,27 @@ async def start(self, cancel_parent_task_on_exception=True):
4343
if self._task in done:
4444
self._task.result()
4545

46-
async def stop(self):
46+
async def stop(self) -> None:
4747
self.cancel()
4848
await self.join()
4949

50-
def cancel(self):
50+
def cancel(self) -> None:
5151
self._task.cancel()
5252

53-
async def join(self):
53+
async def join(self) -> None:
5454
if self._task.done():
5555
self._task.result()
5656
else:
5757
await self._task
5858

59-
async def __run(self):
59+
async def __run(self) -> None:
6060
try:
6161
await self._run()
6262
except asyncio.CancelledError:
6363
pass
6464
except:
6565
if self._started.is_set() and self._cancel_parent_task_on_exception:
66+
assert self._parent_task is not None
6667
self._parent_task.cancel()
6768
raise
6869
finally:

enapter/log/json_formatter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import datetime
2+
import logging
3+
from typing import Any, Dict
24

3-
import json_log_formatter
5+
import json_log_formatter # type: ignore
46

57

68
class JSONFormatter(json_log_formatter.JSONFormatter):
7-
def json_record(self, message, extra, record):
9+
def json_record(
10+
self,
11+
message: str,
12+
extra: Dict[str, Any],
13+
record: logging.LogRecord,
14+
) -> Dict[str, Any]:
815
try:
916
del extra["taskName"]
1017
except KeyError:
@@ -27,5 +34,5 @@ def json_record(self, message, extra, record):
2734

2835
return json_record
2936

30-
def mutate_json_record(self, json_record):
37+
def mutate_json_record(self, json_record: Dict[str, Any]) -> Dict[str, Any]:
3138
return json_record

enapter/mdns/resolver.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import logging
22

3-
import dns.asyncresolver
3+
import dns.asyncresolver # type: ignore
44

55
LOGGER = logging.getLogger(__name__)
66

77

88
class Resolver:
9-
def __init__(self):
9+
def __init__(self) -> None:
1010
self._logger = LOGGER
1111
self._dns_resolver = self._new_dns_resolver()
1212
self._mdns_resolver = self._new_mdns_resolver()
1313

14-
async def resolve(self, host):
14+
async def resolve(self, host: str) -> str:
1515
# TODO: Resolve concurrently.
1616
try:
1717
ip = await self._resolve(self._dns_resolver, host)
@@ -25,17 +25,17 @@ async def resolve(self, host):
2525
self._logger.info("%r resolved using mDNS: %r", host, ip)
2626
return ip
2727

28-
async def _resolve(self, resolver, host):
28+
async def _resolve(self, resolver: dns.asyncresolver.Resolver, host: str) -> str:
2929
answer = await resolver.resolve(host, "A")
3030
if not answer:
3131
raise ValueError(f"empty answer received: {host}")
3232

3333
return answer[0].address
3434

35-
def _new_dns_resolver(self):
35+
def _new_dns_resolver(self) -> dns.asyncresolver.Resolver:
3636
return dns.asyncresolver.Resolver(configure=True)
3737

38-
def _new_mdns_resolver(self):
38+
def _new_mdns_resolver(self) -> dns.asyncresolver.Resolver:
3939
r = dns.asyncresolver.Resolver(configure=False)
4040
r.nameservers = ["224.0.0.251"]
4141
r.port = 5353

enapter/mqtt/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from . import api
22
from .client import Client
3-
from .config import Config
3+
from .config import Config, TLSConfig
44

55
__all__ = [
6-
"api",
76
"Client",
87
"Config",
8+
"TLSConfig",
9+
"api",
910
]

enapter/mqtt/api/command.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import enum
22
import json
3+
from typing import Any, Dict, Optional, Union
34

45

56
class CommandState(enum.Enum):
@@ -9,24 +10,29 @@ class CommandState(enum.Enum):
910

1011
class CommandRequest:
1112
@classmethod
12-
def unmarshal_json(cls, data):
13+
def unmarshal_json(cls, data: Union[str, bytes]) -> "CommandRequest":
1314
req = json.loads(data)
1415
return cls(id_=req["id"], name=req["name"], args=req.get("arguments"))
1516

16-
def __init__(self, id_, name, args=None):
17+
def __init__(self, id_: str, name: str, args: Optional[Dict[str, Any]] = None):
1718
self.id = id_
1819
self.name = name
1920

2021
if args is None:
2122
args = {}
2223
self.args = args
2324

24-
def new_response(self, *args, **kwargs):
25+
def new_response(self, *args, **kwargs) -> "CommandResponse":
2526
return CommandResponse(self.id, *args, **kwargs)
2627

2728

2829
class CommandResponse:
29-
def __init__(self, id_, state, payload=None):
30+
def __init__(
31+
self,
32+
id_: str,
33+
state: Union[str, CommandState],
34+
payload: Optional[Union[Dict[str, Any], str]] = None,
35+
) -> None:
3036
self.id = id_
3137

3238
if not isinstance(state, CommandState):
@@ -37,8 +43,8 @@ def __init__(self, id_, state, payload=None):
3743
payload = {"message": payload}
3844
self.payload = payload
3945

40-
def json(self):
41-
json_object = {"id": self.id, "state": self.state.value}
46+
def json(self) -> Dict[str, Any]:
47+
json_object: Dict[str, Any] = {"id": self.id, "state": self.state.value}
4248
if self.payload is not None:
4349
json_object["payload"] = self.payload
4450

0 commit comments

Comments
 (0)