From edfebb0f2cdb9b6dd842ca8cdda2b472cf6fad45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= Date: Sun, 14 Jan 2024 19:43:26 -0500 Subject: [PATCH] refactor: Default to None for Client arguments --- aiomqtt/client.py | 19 ++++++++++++------- docs/migration-guide-v2.md | 6 ++++-- tests/test_client.py | 1 + tests/test_topic.py | 8 +++----- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/aiomqtt/client.py b/aiomqtt/client.py index 51812f3..ec3e4f6 100644 --- a/aiomqtt/client.py +++ b/aiomqtt/client.py @@ -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. @@ -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, @@ -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, @@ -227,10 +228,14 @@ def __init__( # noqa: C901, PLR0912, PLR0913, PLR0915 self._misc_task: asyncio.Task[None] | None = None # Queue that holds incoming messages + if queue_type is None: + queue_type = asyncio.Queue[Message] + if max_queued_incoming_messages is None: + max_queued_incoming_messages = 0 self._queue: asyncio.Queue[Message] = queue_type( maxsize=max_queued_incoming_messages ) - self.messages: AsyncGenerator[Message, None] = self._messages() + self.messages = self._messages() # Semaphore to limit the number of concurrent outgoing calls self._outgoing_calls_sem: asyncio.Semaphore | None diff --git a/docs/migration-guide-v2.md b/docs/migration-guide-v2.md index 707233e..f31e3fa 100644 --- a/docs/migration-guide-v2.md +++ b/docs/migration-guide-v2.md @@ -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()) diff --git a/tests/test_client.py b/tests/test_client.py index 7c96d42..58c4443 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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" diff --git a/tests/test_topic.py b/tests/test_topic.py index 2abb2b9..023077e 100644 --- a/tests/test_topic.py +++ b/tests/test_topic.py @@ -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] @@ -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] @@ -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")