Skip to content

Commit 9a8c5a4

Browse files
committed
refactor: Adapt to new ruff rules
1 parent deb950d commit 9a8c5a4

9 files changed

+154
-159
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Note that the underlying paho-mqtt library is dual-licensed. One of the licenses
7373

7474
## Contributing
7575

76-
We're happy about contributions to aiomqtt! 🎉 You can get started by reading [CONTRIBUTING.md](https://github.com/empicano/aiomqtt/blob/main/CONTRIBUTING.md).
76+
We're super happy about contributions to aiomqtt! 🎉 Get started by reading [CONTRIBUTING.md](https://github.com/empicano/aiomqtt/blob/main/CONTRIBUTING.md).
7777

7878
## Versioning
7979

aiomqtt/client.py

+96-63
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
from .message import Message
3737
from .types import (
3838
P,
39+
PahoSocket,
3940
PayloadType,
4041
SocketOption,
4142
SubscribeTopic,
4243
T,
4344
WebSocketHeaders,
44-
_PahoSocket,
4545
)
4646

4747
if sys.version_info >= (3, 11):
@@ -178,7 +178,8 @@ class Client:
178178
client when it disconnects. If ``False``, the client is a persistent client
179179
and subscription information and queued messages will be retained when the
180180
client disconnects.
181-
transport: The transport protocol to use. Either ``"tcp"``, ``"websockets"`` or ``"unix"``.
181+
transport: The transport protocol to use. Either ``"tcp"``, ``"websockets"`` or
182+
``"unix"``.
182183
timeout: The default timeout for all communication with the broker in seconds.
183184
keepalive: The keepalive timeout for the client in seconds.
184185
bind_address: The IP address of a local network interface to bind this client
@@ -205,7 +206,7 @@ class Client:
205206
websocket_headers: The headers to use for websockets.
206207
"""
207208

208-
def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915
209+
def __init__(
209210
self,
210211
hostname: str,
211212
port: int = 1883,
@@ -253,7 +254,8 @@ def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915
253254

254255
# Pending subscribe, unsubscribe, and publish calls
255256
self._pending_subscribes: dict[
256-
int, asyncio.Future[tuple[int, ...] | list[ReasonCode]]
257+
int,
258+
asyncio.Future[tuple[int, ...] | list[ReasonCode]],
257259
] = {}
258260
self._pending_unsubscribes: dict[int, asyncio.Event] = {}
259261
self._pending_publishes: dict[int, asyncio.Event] = {}
@@ -337,7 +339,11 @@ def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915
337339

338340
if will is not None:
339341
self._client.will_set(
340-
will.topic, will.payload, will.qos, will.retain, will.properties
342+
will.topic,
343+
will.payload,
344+
will.qos,
345+
will.retain,
346+
will.properties,
341347
)
342348

343349
if socket_options is None:
@@ -377,9 +383,9 @@ async def subscribe(
377383
qos: int = 0,
378384
options: SubscribeOptions | None = None,
379385
properties: Properties | None = None,
380-
*args: Any,
386+
*args: Any, # noqa: ANN401
381387
timeout: float | None = None,
382-
**kwargs: Any,
388+
**kwargs: Any, # noqa: ANN401
383389
) -> tuple[int, ...] | list[ReasonCode]:
384390
"""Subscribe to a topic or wildcard.
385391
@@ -397,7 +403,12 @@ async def subscribe(
397403
398404
"""
399405
result, mid = self._client.subscribe(
400-
topic, qos, options, properties, *args, **kwargs
406+
topic,
407+
qos,
408+
options,
409+
properties,
410+
*args,
411+
**kwargs,
401412
)
402413
# Early out on error
403414
if result != mqtt.MQTT_ERR_SUCCESS or mid is None:
@@ -416,9 +427,9 @@ async def unsubscribe(
416427
/,
417428
topic: str | list[str],
418429
properties: Properties | None = None,
419-
*args: Any,
430+
*args: Any, # noqa: ANN401
420431
timeout: float | None = None,
421-
**kwargs: Any,
432+
**kwargs: Any, # noqa: ANN401
422433
) -> None:
423434
"""Unsubscribe from a topic or wildcard.
424435
@@ -443,17 +454,17 @@ async def unsubscribe(
443454
await self._wait_for(confirmation.wait(), timeout=timeout)
444455

445456
@_outgoing_call
446-
async def publish( # noqa: PLR0913
457+
async def publish(
447458
self,
448459
/,
449460
topic: str,
450461
payload: PayloadType = None,
451462
qos: int = 0,
452-
retain: bool = False,
463+
retain: bool = False, # noqa: FBT001, FBT002
453464
properties: Properties | None = None,
454-
*args: Any,
465+
*args: Any, # noqa: ANN401
455466
timeout: float | None = None,
456-
**kwargs: Any,
467+
**kwargs: Any, # noqa: ANN401
457468
) -> None:
458469
"""Publish a message to the broker.
459470
@@ -471,7 +482,13 @@ async def publish( # noqa: PLR0913
471482
method.
472483
"""
473484
info = self._client.publish(
474-
topic, payload, qos, retain, properties, *args, **kwargs
485+
topic,
486+
payload,
487+
qos,
488+
retain,
489+
properties,
490+
*args,
491+
**kwargs,
475492
) # [2]
476493
# Early out on error
477494
if info.rc != mqtt.MQTT_ERR_SUCCESS:
@@ -485,22 +502,23 @@ async def publish( # noqa: PLR0913
485502
# Wait for confirmation
486503
await self._wait_for(confirmation.wait(), timeout=timeout)
487504

488-
async def _wait_for(
489-
self, fut: Awaitable[T], timeout: float | None, **kwargs: Any
490-
) -> T:
505+
async def _wait_for(self, fut: Awaitable[T], timeout: float | None) -> T:
491506
if timeout is None:
492507
timeout = self.timeout
493508
# Note that asyncio uses `None` to mean "No timeout". We use `math.inf`.
494509
timeout_for_asyncio = None if timeout == math.inf else timeout
495510
try:
496-
return await asyncio.wait_for(fut, timeout=timeout_for_asyncio, **kwargs)
511+
return await asyncio.wait_for(fut, timeout=timeout_for_asyncio)
497512
except asyncio.TimeoutError:
498513
msg = "Operation timed out"
499514
raise MqttError(msg) from None
500515

501516
@contextlib.contextmanager
502517
def _pending_call(
503-
self, mid: int, value: T, pending_dict: dict[int, T]
518+
self,
519+
mid: int,
520+
value: T,
521+
pending_dict: dict[int, T],
504522
) -> Iterator[None]:
505523
if mid in self._pending_calls:
506524
msg = f'There already exists a pending call for message ID "{mid}"'
@@ -520,18 +538,16 @@ def _pending_call(
520538
#
521539
# However, if the callback doesn't get called (e.g., due to a
522540
# network error) we still need to remove the item from the dict.
523-
try:
541+
with contextlib.suppress(KeyError):
524542
del pending_dict[mid]
525-
except KeyError:
526-
pass
527543

528544
def _on_connect(
529545
self,
530-
client: mqtt.Client,
531-
userdata: Any,
532-
flags: mqtt.ConnectFlags,
546+
client: mqtt.Client, # noqa: ARG002
547+
userdata: Any, # noqa: ARG002, ANN401
548+
flags: mqtt.ConnectFlags, # noqa: ARG002
533549
reason_code: ReasonCode,
534-
properties: Properties | None = None,
550+
properties: Properties | None = None, # noqa: ARG002
535551
) -> None:
536552
"""Called when we receive a CONNACK message from the broker."""
537553
# Return early if already connected. Sometimes, paho-mqtt calls _on_connect
@@ -548,11 +564,11 @@ def _on_connect(
548564

549565
def _on_disconnect(
550566
self,
551-
client: mqtt.Client,
552-
userdata: Any,
553-
flags: mqtt.DisconnectFlags,
567+
client: mqtt.Client, # noqa: ARG002
568+
userdata: Any, # noqa: ARG002, ANN401
569+
flags: mqtt.DisconnectFlags, # noqa: ARG002
554570
reason_code: ReasonCode,
555-
properties: Properties | None = None,
571+
properties: Properties | None = None, # noqa: ARG002
556572
) -> None:
557573
# Return early if the disconnect is already acknowledged.
558574
# Sometimes (e.g., due to timeouts), paho-mqtt calls _on_disconnect
@@ -575,16 +591,16 @@ def _on_disconnect(
575591
self._disconnected.set_result(None)
576592
else:
577593
self._disconnected.set_exception(
578-
MqttCodeError(reason_code, "Unexpected disconnection")
594+
MqttCodeError(reason_code, "Unexpected disconnection"),
579595
)
580596

581597
def _on_subscribe(
582598
self,
583-
client: mqtt.Client,
584-
userdata: Any,
599+
client: mqtt.Client, # noqa: ARG002
600+
userdata: Any, # noqa: ARG002, ANN401
585601
mid: int,
586602
reason_codes: list[ReasonCode],
587-
properties: Properties | None = None,
603+
properties: Properties | None = None, # noqa: ARG002
588604
) -> None:
589605
"""Called when we receive a SUBACK message from the broker."""
590606
try:
@@ -593,27 +609,32 @@ def _on_subscribe(
593609
fut.set_result(reason_codes)
594610
except KeyError:
595611
self._logger.exception(
596-
'Unexpected message ID "%d" in on_subscribe callback', mid
612+
'Unexpected message ID "%d" in on_subscribe callback',
613+
mid,
597614
)
598615

599616
def _on_unsubscribe(
600617
self,
601-
client: mqtt.Client,
602-
userdata: Any,
618+
client: mqtt.Client, # noqa: ARG002
619+
userdata: Any, # noqa: ARG002, ANN401
603620
mid: int,
604-
reason_codes: list[ReasonCode],
605-
properties: Properties | None = None,
621+
reason_codes: list[ReasonCode], # noqa: ARG002
622+
properties: Properties | None = None, # noqa: ARG002
606623
) -> None:
607624
"""Called when we receive an UNSUBACK message from the broker."""
608625
try:
609626
self._pending_unsubscribes.pop(mid).set()
610627
except KeyError:
611628
self._logger.exception(
612-
'Unexpected message ID "%d" in on_unsubscribe callback', mid
629+
'Unexpected message ID "%d" in on_unsubscribe callback',
630+
mid,
613631
)
614632

615633
def _on_message(
616-
self, client: mqtt.Client, userdata: Any, message: mqtt.MQTTMessage
634+
self,
635+
client: mqtt.Client, # noqa: ARG002
636+
userdata: Any, # noqa: ARG002, ANN401
637+
message: mqtt.MQTTMessage,
617638
) -> None:
618639
# Convert the paho.mqtt message into our own Message type
619640
m = Message._from_paho_message(message) # noqa: SLF001
@@ -625,30 +646,31 @@ def _on_message(
625646

626647
def _on_publish(
627648
self,
628-
client: mqtt.Client,
629-
userdata: Any,
649+
client: mqtt.Client, # noqa: ARG002
650+
userdata: Any, # noqa: ARG002, ANN401
630651
mid: int,
631-
reason_code: ReasonCode,
632-
properties: Properties,
652+
reason_code: ReasonCode, # noqa: ARG002
653+
properties: Properties, # noqa: ARG002
633654
) -> None:
634-
try:
655+
# Suppress KeyError since [2] may call on_publish before it even returns.
656+
# That is, the message may already be published before we even get a chance to
657+
# set up the 'pending_call' logic.
658+
with contextlib.suppress(KeyError):
635659
self._pending_publishes.pop(mid).set()
636-
except KeyError:
637-
# Do nothing since [2] may call on_publish before it even returns.
638-
# That is, the message may already be published before we even get a
639-
# chance to set up the 'pending_call' logic.
640-
pass
641660

642661
def _on_socket_open(
643-
self, client: mqtt.Client, userdata: Any, sock: _PahoSocket
662+
self,
663+
client: mqtt.Client,
664+
userdata: Any, # noqa: ARG002, ANN401
665+
sock: PahoSocket,
644666
) -> None:
645667
def callback() -> None:
646668
# client.loop_read() may raise an exception, such as BadPipe. It's
647669
# usually a sign that the underlying connection broke, therefore we
648670
# disconnect straight away
649671
try:
650672
client.loop_read()
651-
except Exception as exc:
673+
except Exception as exc: # noqa: BLE001
652674
if not self._disconnected.done():
653675
self._disconnected.set_exception(exc)
654676

@@ -662,7 +684,10 @@ def create_misc_task() -> None:
662684
self._loop.call_soon_threadsafe(create_misc_task)
663685

664686
def _on_socket_close(
665-
self, client: mqtt.Client, userdata: Any, sock: _PahoSocket
687+
self,
688+
client: mqtt.Client, # noqa: ARG002
689+
userdata: Any, # noqa: ARG002, ANN401
690+
sock: PahoSocket,
666691
) -> None:
667692
fileno = sock.fileno()
668693
if fileno > -1:
@@ -671,30 +696,36 @@ def _on_socket_close(
671696
self._loop.call_soon_threadsafe(self._misc_task.cancel)
672697

673698
def _on_socket_register_write(
674-
self, client: mqtt.Client, userdata: Any, sock: _PahoSocket
699+
self,
700+
client: mqtt.Client,
701+
userdata: Any, # noqa: ARG002, ANN401
702+
sock: PahoSocket,
675703
) -> None:
676704
def callback() -> None:
677705
# client.loop_write() may raise an exception, such as BadPipe. It's
678706
# usually a sign that the underlying connection broke, therefore we
679707
# disconnect straight away
680708
try:
681709
client.loop_write()
682-
except Exception as exc:
710+
except Exception as exc: # noqa: BLE001
683711
if not self._disconnected.done():
684712
self._disconnected.set_exception(exc)
685713

686-
# paho-mqtt may call this function from the executor thread on which we've called
687-
# `self._client.connect()` (see [3]), so we can't do most operations on
714+
# paho-mqtt may call this function from the executor thread on which we've
715+
# called `self._client.connect()` (see [3]), so we can't do most operations on
688716
# self._loop directly.
689717
self._loop.call_soon_threadsafe(self._loop.add_writer, sock.fileno(), callback)
690718

691719
def _on_socket_unregister_write(
692-
self, client: mqtt.Client, userdata: Any, sock: _PahoSocket
720+
self,
721+
client: mqtt.Client, # noqa: ARG002
722+
userdata: Any, # noqa: ARG002, ANN401
723+
sock: PahoSocket,
693724
) -> None:
694725
self._loop.remove_writer(sock.fileno())
695726

696727
async def _misc_loop(self) -> None:
697-
while self._client.loop_misc() == mqtt.MQTT_ERR_SUCCESS:
728+
while self._client.loop_misc() == mqtt.MQTT_ERR_SUCCESS: # noqa: ASYNC110
698729
await asyncio.sleep(1)
699730

700731
async def __aenter__(self) -> Self:
@@ -761,7 +792,8 @@ async def __aexit__(
761792
self._connected = asyncio.Future()
762793
else:
763794
self._logger.warning(
764-
"Could not gracefully disconnect: %d. Forcing disconnection.", rc
795+
"Could not gracefully disconnect: %d. Forcing disconnection.",
796+
rc,
765797
)
766798
# Force disconnection if we cannot gracefully disconnect
767799
if not self._disconnected.done():
@@ -772,7 +804,8 @@ async def __aexit__(
772804

773805

774806
def _set_client_socket_defaults(
775-
client_socket: _PahoSocket | None, socket_options: Iterable[SocketOption]
807+
client_socket: PahoSocket | None,
808+
socket_options: Iterable[SocketOption],
776809
) -> None:
777810
# Note that socket may be None if, e.g., the username and
778811
# password combination didn't work. In this case, we return early.

0 commit comments

Comments
 (0)