Skip to content

Commit

Permalink
refactor: Default to None for Client arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
empicano committed Jan 15, 2024
1 parent 7ede503 commit be0fddf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
23 changes: 13 additions & 10 deletions aiomqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ class Client:
clean_start: (MQTT v5.0 only) Set the clean start flag always, never, or only
on the first successful connection to the broker.
max_queued_incoming_messages: Restricts the incoming message queue size. If the
queue is full, incoming messages will be discarded and a warning logged.
If set to ``0`` or less, the queue size is infinite.
max_queued_outgoing_messages: The maximum number of messages in the outgoing
message queue. ``0`` means unlimited.
queue is full, further incoming messages are discarded. ``0`` or less means
unlimited (the default).
max_queued_outgoing_messages: Resticts the outgoing message queue size. If the
queue is full, further outgoing messages are discarded. ``0`` means
unlimited (the default).
max_inflight_messages: The maximum number of messages with QoS > ``0`` that can
be part way through their network flow at once.
max_concurrent_outgoing_calls: The maximum number of concurrent outgoing calls.
Expand Down Expand Up @@ -180,7 +181,7 @@ def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915
password: str | None = None,
logger: logging.Logger | None = None,
identifier: str | None = None,
queue_type: type[asyncio.Queue[Message]] = asyncio.Queue,
queue_type: type[asyncio.Queue[Message]] | None = None,
protocol: ProtocolVersion | None = None,
will: Will | None = None,
clean_session: bool | None = None,
Expand All @@ -190,7 +191,7 @@ def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915
bind_address: str = "",
bind_port: int = 0,
clean_start: int = mqtt.MQTT_CLEAN_START_FIRST_ONLY,
max_queued_incoming_messages: int = 0,
max_queued_incoming_messages: int | None = None,
max_queued_outgoing_messages: int | None = None,
max_inflight_messages: int | None = None,
max_concurrent_outgoing_calls: int | None = None,
Expand Down Expand Up @@ -227,10 +228,12 @@ def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915
self._misc_task: asyncio.Task[None] | None = None

# Queue that holds incoming messages
self._queue: asyncio.Queue[Message] = queue_type(
maxsize=max_queued_incoming_messages
)
self.messages: AsyncGenerator[Message, None] = self._messages()
if queue_type is None:
queue_type = cast("type[asyncio.Queue[Message]]", asyncio.Queue)
if max_queued_incoming_messages is None:
max_queued_incoming_messages = 0
self._queue = queue_type(maxsize=max_queued_incoming_messages)
self.messages = self._messages()

# Semaphore to limit the number of concurrent outgoing calls
self._outgoing_calls_sem: asyncio.Semaphore | None
Expand Down
6 changes: 4 additions & 2 deletions docs/migration-guide-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import aiomqtt
async def main():
client = aiomqtt.Client("test.mosquitto.org")
await client.__aenter__()
await client.publish("temperature/outside", payload=28.4)
await client.__aexit__(None, None, None)
try:
await client.publish("temperature/outside", payload=28.4)
finally:
await client.__aexit__(None, None, None)


asyncio.run(main())
Expand Down
1 change: 1 addition & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from aiomqtt.types import PayloadType

# This is the same as marking all tests in this file with @pytest.mark.anyio
pytestmark = pytest.mark.anyio

HOSTNAME = "test.mosquitto.org"
Expand Down
8 changes: 3 additions & 5 deletions tests/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

from aiomqtt import Topic, Wildcard

pytestmark = pytest.mark.anyio


async def test_topic_validation() -> None:
def test_topic_validation() -> None:
"""Test that Topic raises Exceptions for invalid topics."""
with pytest.raises(TypeError):
Topic(True) # type: ignore[arg-type]
Expand All @@ -29,7 +27,7 @@ async def test_topic_validation() -> None:
Topic("a" * 65536)


async def test_wildcard_validation() -> None:
def test_wildcard_validation() -> None:
"""Test that Wildcard raises Exceptions for invalid wildcards."""
with pytest.raises(TypeError):
Wildcard(True) # type: ignore[arg-type]
Expand All @@ -51,7 +49,7 @@ async def test_wildcard_validation() -> None:
Wildcard("a" * 65536)


async def test_topic_matches() -> None:
def test_topic_matches() -> None:
"""Test that Topic.matches() does and doesn't match some test wildcards."""
topic = Topic("a/b/c")
assert topic.matches("a/b/c")
Expand Down

0 comments on commit be0fddf

Please sign in to comment.