Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Remove magic methods for hashing, use names for queue/exchange #2050

Open
wants to merge 3 commits into
base: 0.6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions faststream/rabbit/helpers/declarer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class RabbitDeclarer:
"""An utility class to declare RabbitMQ queues and exchanges."""

def __init__(self) -> None:
self.__queues: dict[RabbitQueue, aio_pika.RobustQueue] = {}
self.__exchanges: dict[RabbitExchange, aio_pika.RobustExchange] = {}
self.__queues: dict[str, aio_pika.RobustQueue] = {}
self.__exchanges: dict[str, aio_pika.RobustExchange] = {}

self.__connection: ConnectionState = EmptyConnectionState()

Expand All @@ -36,8 +36,8 @@ async def declare_queue(
passive: bool = False,
) -> "aio_pika.RobustQueue":
"""Declare a queue."""
if (q := self.__queues.get(queue)) is None:
self.__queues[queue] = q = cast(
if (q := self.__queues.get(queue.name)) is None:
self.__queues[queue.name] = q = cast(
"aio_pika.RobustQueue",
await self.__connection.channel.declare_queue(
name=queue.name,
Expand All @@ -50,8 +50,11 @@ async def declare_queue(
robust=queue.robust,
),
)

return q
if all([getattr(queue, attr)==getattr(q, attr) for attr in ["name", "durable", "exclusive", "passive",
"auto_delete", "arguments"]]):
return q
else:
raise ValueError("Queue mismatch")

async def declare_exchange(
self,
Expand All @@ -62,8 +65,8 @@ async def declare_exchange(
if not exchange.name:
return self.__connection.channel.default_exchange

if (exch := self.__exchanges.get(exchange)) is None:
self.__exchanges[exchange] = exch = cast(
if (exch := self.__exchanges.get(exchange.name)) is None:
self.__exchanges[exchange.name] = exch = cast(
"aio_pika.RobustExchange",
await self.__connection.channel.declare_exchange(
name=exchange.name,
Expand All @@ -87,5 +90,7 @@ async def declare_exchange(
timeout=exchange.timeout,
robust=exchange.robust,
)

return exch
if self.__exchanges[exchange.name]==exch:
return exch
else:
raise ValueError("Exchange mismatch")
12 changes: 0 additions & 12 deletions faststream/rabbit/schemas/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ def __repr__(self) -> str:

return f"{self.__class__.__name__}({self.name}, type={self.type}, routing_key='{self.routing}'{body})"

def __hash__(self) -> int:
"""Supports hash to store real objects in declarer."""
return sum(
(
hash(self.name),
hash(self.type),
hash(self.routing_key),
int(self.durable),
int(self.auto_delete),
),
)

@property
def routing(self) -> str:
"""Return real routing_key of object."""
Expand Down
11 changes: 0 additions & 11 deletions faststream/rabbit/schemas/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ def __repr__(self) -> str:

return f"{self.__class__.__name__}({self.name}, routing_key='{self.routing}'{body})"

def __hash__(self) -> int:
"""Supports hash to store real objects in declarer."""
return sum(
(
hash(self.name),
int(self.durable),
int(self.exclusive),
int(self.auto_delete),
),
)

@property
def routing(self) -> str:
"""Return real routing_key of object."""
Expand Down
Loading