Skip to content

Commit

Permalink
chore: Make ruff stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Sep 5, 2024
1 parent 8c73dd9 commit 776d155
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
rev: v0.6.3
hooks:
- id: ruff
- id: ruff-format
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ ignore = [
"W191", "E501", "D206", "Q000", "Q001", "Q002", "Q003", "ISC001",
"D203", "D212", # incompatible rules
"D200", # documentation preferences
"D100", "D104", "D205", "D400", "D415",
"C901", "PLR091", # complexity preferences
"D100", "D104", # docstrings
"ARG001", "ARG002", # unused arguments (callbacks)
"PLR0913", # many arguments (decorators)
"PYI024", # Python 3.10 has no generic namedtuples
]

Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@


# Use this in tests that terminate naturally (e.g. due to an exception).
@pytest.fixture()
@pytest.fixture
def timer(request):
with timed(30):
yield


# Use this in tests that don't terminate naturally.
@pytest.fixture()
@pytest.fixture
def short_timer(request):
with timed(0.05):
yield
Expand All @@ -31,7 +31,7 @@ def message(request):
publisher.close()


@pytest.fixture()
@pytest.fixture
def short_message(request):
body = 1

Expand All @@ -44,7 +44,7 @@ def short_message(request):
publisher.close()


@pytest.fixture()
@pytest.fixture
def short_reconnect_delay(request):
reconnect_delay = Async.RECONNECT_DELAY
Async.RECONNECT_DELAY = 1
Expand Down
41 changes: 26 additions & 15 deletions yapw/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def __init__(
routing_key_template: str = "{exchange}_{routing_key}",
):
"""
Initialize the client's state.
:param url: the connection string (don't set a ``blocked_connection_timeout`` query string parameter)
:param blocked_connection_timeout: the timeout, in seconds, that the connection may remain blocked
:param durable: whether to declare a durable exchange, declare durable queues, and publish persistent messages
Expand Down Expand Up @@ -177,6 +179,8 @@ def add_signal_handlers(self, handler: Callable[..., object]) -> None:

def add_signal_handler(self, signalnum: int, handler: Callable[..., object]) -> None:
"""
Add a handler for a signal.
Override this method in subclasses to add a handler for a signal (e.g. using :func:`signal.signal` or
:meth:`asyncio.loop.add_signal_handler`). The handler should remove signal handlers (in order to ignore
duplicate signals), log a message with a level of ``INFO``, and call :meth:`yapw.clients.base.interrupt`.
Expand Down Expand Up @@ -205,8 +209,7 @@ class Blocking(Base[pika.BlockingConnection]):

def __init__(self, **kwargs: Any):
"""
Connect to RabbitMQ, create a channel, set the prefetch count, and declare an exchange, unless using the
default exchange.
Connect to RabbitMQ, create a channel, set the prefetch count, and declare an exchange.
"""
super().__init__(**kwargs)

Expand All @@ -226,8 +229,9 @@ def declare_queue(
self, queue: str, routing_keys: list[str] | None = None, arguments: dict[str, str] | None = None
) -> None:
"""
Declare a queue, and bind it to the exchange with the routing keys. If no routing keys are provided, the queue
is bound to the exchange using its name as the routing key.
Declare a queue, and bind it to the exchange with the routing keys.
If no routing keys are provided, the queue is bound to the exchange using its name as the routing key.
:param queue: the queue's name
:param routing_keys: the queue's routing keys
Expand Down Expand Up @@ -328,8 +332,9 @@ def close(self) -> None:

class Async(Base[AsyncioConnection]):
"""
Uses Pika's :class:`AsyncioConnection adapter<pika.adapters.asyncio_connection.AsyncioConnection>`. Reconnects to
RabbitMQ if the connection is closed unexpectedly or can't be established.
Uses Pika's :class:`AsyncioConnection adapter<pika.adapters.asyncio_connection.AsyncioConnection>`.
Reconnects to RabbitMQ if the connection is closed unexpectedly or can't be established.
Calling :meth:`~yapw.clients.Async.start` connects to RabbitMQ, add signal handlers, and starts the IO loop.
Expand Down Expand Up @@ -386,8 +391,7 @@ def start(self) -> None:

def connect(self) -> None:
"""
Connect to RabbitMQ, create a channel, set the prefetch count, and declare an exchange, unless using the
default exchange.
Connect to RabbitMQ, create a channel, set the prefetch count, and declare an exchange.
"""
self.connection = AsyncioConnection(
self.parameters,
Expand Down Expand Up @@ -471,8 +475,11 @@ def _on_signal_callback(self, signalnum: int) -> None:

def interrupt(self) -> None:
"""
`Cancel <https://www.rabbitmq.com/consumers.html#unsubscribing>`__ the consumer if consuming and if the channel
is open. Otherwise, wait for threads to terminate and close the connection.
`Cancel`_ the consumer if consuming and if the channel is open.
Otherwise, wait for threads to terminate and close the connection.
.. _Cancel: https://www.rabbitmq.com/consumers.html#unsubscribing
"""
# Change the client's state to stopping, to prevent infinite reconnection.
self.stopping = True
Expand All @@ -496,17 +503,19 @@ def channel_open_callback(self, channel: pika.channel.Channel) -> None:

def channel_cancelok_callback(self, method: pika.frame.Method[pika.spec.Basic.CancelOk]) -> Any:
"""
Close the channel, once the consumer is cancelled. The :meth:`~yapw.clients.Async.channel_close_callback`
closes the connection.
Close the channel, once the consumer is cancelled.
The :meth:`~yapw.clients.Async.channel_close_callback` closes the connection.
"""
# Keep channel open until threads terminate. Ensure the channel closes after any thread-safe callbacks.
self.executor.shutdown(cancel_futures=True)
self.connection.ioloop.call_later(0, self.channel.close)

def channel_close_callback(self, channel: pika.channel.Channel, reason: Exception) -> None:
"""
Close the connection, once the client cancelled the consumer or once RabbitMQ closed the channel due to, e.g.,
redeclaring exchanges with inconsistent parameters.
Close the connection, once the client cancelled the consumer or once RabbitMQ closed the channel.
RabbitMQ can close the channel due to, e.g., redeclaring exchanges with inconsistent parameters.
A warning is logged, in case it was the latter.
"""
Expand All @@ -518,7 +527,7 @@ def channel_close_callback(self, channel: pika.channel.Channel, reason: Exceptio
self.connection.close()

def channel_qosok_callback(self, method: pika.frame.Method[pika.spec.Basic.QosOk]) -> None:
"""Declare the exchange, once the prefetch count is set."""
"""Declare the exchange, once the prefetch count is set, if not using the default exchange."""
if self.exchange:
self.channel.exchange_declare(
exchange=self.exchange,
Expand Down Expand Up @@ -563,6 +572,8 @@ def __init__(
**kwargs: Any,
) -> None:
"""
Initialize the client's state.
.. seealso::
:meth:`yapw.clients.AsyncConsumer.consume`
Expand Down
5 changes: 3 additions & 2 deletions yapw/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def json_dumps(message: Any) -> bytes:

def default_decode(body: bytes, content_type: str | None) -> Any:
"""
If the content type is "application/json", deserializes the JSON formatted bytes to a Python object. Otherwise,
returns the bytes (which the consumer callback can deserialize independently).
If the content type is "application/json", deserialize the JSON formatted bytes to a Python object.
Otherwise, return the bytes (which the consumer callback can deserialize independently).
Uses `orjson <https://pypi.org/project/orjson/>`__ if available.
Expand Down

0 comments on commit 776d155

Please sign in to comment.