Skip to content

Commit 1e230ea

Browse files
committed
Make handling of bytes and bytesarray args more consistent
Fixes: src\paho\mqtt\client.py:2233: error: Incompatible types in assignment (expression has type "bytes | bytearray", variable has type "bytes") [assignment] src\paho\mqtt\client.py:3444: error: Argument 2 to "_packet_queue" of "Client" has incompatible type "bytearray"; expected "bytes" [arg-type] src\paho\mqtt\client.py:3572: error: Argument 2 to "_packet_queue" of "Client" has incompatible type "bytearray"; expected "bytes" [arg-type] src\paho\mqtt\client.py:3610: error: Argument 2 to "_packet_queue" of "Client" has incompatible type "bytearray"; expected "bytes" [arg-type] src\paho\mqtt\client.py:3652: error: Argument 2 to "_packet_queue" of "Client" has incompatible type "bytearray"; expected "bytes" [arg-type] src\paho\mqtt\client.py:3701: error: Argument 2 to "_packet_queue" of "Client" has incompatible type "bytearray"; expected "bytes" [arg-type] src\paho\mqtt\client.py:4628: error: Module has no attribute "AF_UNIX"; maybe "AF_IPX"? [attr-defined] src\paho\mqtt\client.py:4921: error: Incompatible types in assignment (expression has type "bytearray", variable has type "bytes") [assignment]
1 parent 631a977 commit 1e230ea

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

src/paho/mqtt/client.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import urllib.request
3636
import uuid
3737
import warnings
38-
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, NamedTuple, Sequence, Tuple, Union, cast
38+
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, NamedTuple, Sequence, Tuple, Union, cast, overload
3939

4040
from paho.mqtt.packettypes import PacketTypes
4141

@@ -78,13 +78,13 @@ class _OutPacket(TypedDict):
7878
qos: int
7979
pos: int
8080
to_process: int
81-
packet: bytes
81+
packet: bytes | bytearray
8282
info: MQTTMessageInfo | None
8383

8484
class SocketLike(Protocol):
8585
def recv(self, buffer_size: int) -> bytes:
8686
...
87-
def send(self, buffer: bytes) -> int:
87+
def send(self, buffer: bytes | bytearray) -> int:
8888
...
8989
def close(self) -> None:
9090
...
@@ -458,8 +458,13 @@ def _socketpair_compat() -> tuple[socket.socket, socket.socket]:
458458
listensock.close()
459459
return (sock1, sock2)
460460

461-
462-
def _force_bytes(s: str | bytes) -> bytes:
461+
@overload
462+
def _force_bytes(s: str) -> bytes: ...
463+
@overload
464+
def _force_bytes(s: bytes) -> bytes: ...
465+
@overload
466+
def _force_bytes(s: bytearray) -> bytearray: ...
467+
def _force_bytes(s: str | bytes | bytearray) -> bytes | bytearray:
463468
if isinstance(s, str):
464469
return s.encode("utf-8")
465470
return s
@@ -832,7 +837,7 @@ def __init__(
832837
self._will_properties: Properties | None = None
833838
self._will = False
834839
self._will_topic = b""
835-
self._will_payload = b""
840+
self._will_payload: bytes | bytearray = b""
836841
self._will_qos = 0
837842
self._will_retain = False
838843
self._on_message_filtered = MQTTMatcher()
@@ -1074,7 +1079,7 @@ def will_topic(self) -> str | None:
10741079
return self._will_topic.decode("utf-8")
10751080

10761081
@property
1077-
def will_payload(self) -> bytes | None:
1082+
def will_payload(self) -> bytes | bytearray | None:
10781083
"""
10791084
The payload for the will message that is sent when disconnecting unexpectedly. None if a will shall not be sent.
10801085
@@ -1105,7 +1110,7 @@ def _sock_recv(self, bufsize: int) -> bytes:
11051110
MQTT_LOG_DEBUG, "socket was None: %s", err)
11061111
raise ConnectionError() from err
11071112

1108-
def _sock_send(self, buf: bytes) -> int:
1113+
def _sock_send(self, buf: bytes | bytearray) -> int:
11091114
if self._sock is None:
11101115
raise ConnectionError("self._sock is None")
11111116

@@ -3359,7 +3364,7 @@ def _pack_remaining_length(
33593364
# FIXME - this doesn't deal with incorrectly large payloads
33603365
return packet
33613366

3362-
def _pack_str16(self, packet: bytearray, data: bytes | str) -> None:
3367+
def _pack_str16(self, packet: bytearray, data: bytearray | bytes | str) -> None:
33633368
data = _force_bytes(data)
33643369
packet.extend(struct.pack("!H", len(data)))
33653370
packet.extend(data)
@@ -4918,7 +4923,7 @@ def _recv_impl(self, length: int) -> bytes:
49184923
for index in range(chunk_startindex, readindex):
49194924
payload[index] ^= mask_key[index % 4]
49204925

4921-
result = payload[chunk_startindex:readindex]
4926+
result = bytes(payload[chunk_startindex:readindex])
49224927
self._payload_head = readindex
49234928
else:
49244929
payload = bytearray()
@@ -4951,7 +4956,7 @@ def _recv_impl(self, length: int) -> bytes:
49514956
self.connected = False
49524957
return b''
49534958

4954-
def _send_impl(self, data: bytes) -> int:
4959+
def _send_impl(self, data: bytes | bytearray) -> int:
49554960

49564961
# if previous frame was sent successfully
49574962
if len(self._sendbuffer) == 0:
@@ -4979,7 +4984,7 @@ def recv(self, length: int) -> bytes:
49794984
def read(self, length: int) -> bytes:
49804985
return self._recv_impl(length)
49814986

4982-
def send(self, data: bytes) -> int:
4987+
def send(self, data: bytes | bytearray) -> int:
49834988
return self._send_impl(data)
49844989

49854990
def write(self, data: bytes) -> int:

0 commit comments

Comments
 (0)