Coverage for faststream/rabbit/schemas/exchange.py: 94%
32 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import warnings
2from typing import TYPE_CHECKING, Any, Optional, Union
4from typing_extensions import override
6from faststream._internal.proto import NameRequired
7from faststream.rabbit.schemas.constants import ExchangeType
9if TYPE_CHECKING:
10 from aio_pika.abc import TimeoutType
13class RabbitExchange(NameRequired):
14 """A class to represent a RabbitMQ exchange."""
16 __slots__ = (
17 "arguments",
18 "auto_delete",
19 "bind_arguments",
20 "bind_to",
21 "declare",
22 "durable",
23 "robust",
24 "routing_key",
25 "timeout",
26 "type",
27 )
29 def __repr__(self) -> str:
30 if self.declare:
31 body = f", robust={self.robust}, durable={self.durable}, auto_delete={self.auto_delete})"
32 else:
33 body = ""
35 return f"{self.__class__.__name__}({self.name}, type={self.type}, routing_key='{self.routing()}'{body})"
37 def __eq__(self, value: object, /) -> bool:
38 if not isinstance(value, RabbitExchange):
39 return NotImplemented
41 return (
42 self.name == value.name
43 and self.type == value.type
44 and self.durable == value.durable
45 and self.auto_delete == value.auto_delete
46 and (self.arguments or {}) == (value.arguments or {})
47 )
49 def __hash__(self) -> int:
50 """Supports hash to store real objects in declarer."""
52 def _hash_dict(d: Any) -> Any:
53 if isinstance(d, dict): 53 ↛ 55line 53 didn't jump to line 55 because the condition on line 53 was always true
54 return frozenset((k, _hash_dict(v)) for k, v in d.items())
55 return d
57 return hash(
58 (
59 self.name,
60 self.type,
61 self.durable,
62 self.auto_delete,
63 _hash_dict(self.arguments or {}),
64 ),
65 )
67 def routing(self) -> str:
68 """Return real routing_key of object."""
69 return self.routing_key or self.name
71 def __init__(
72 self,
73 name: str = "",
74 type: ExchangeType = ExchangeType.DIRECT,
75 durable: bool = True,
76 auto_delete: bool = False,
77 # custom
78 declare: bool = True,
79 arguments: dict[str, Any] | None = None,
80 timeout: "TimeoutType" = None,
81 robust: bool = True,
82 bind_to: Optional["RabbitExchange"] = None,
83 bind_arguments: dict[str, Any] | None = None,
84 routing_key: str = "",
85 ) -> None:
86 """Initialize a RabbitExchange object.
88 Args:
89 name: RabbitMQ exchange name.
90 type: RabbitMQ exchange type.
91 durable: Whether the object is durable.
92 auto_delete: The exchange will be deleted after connection closed.
93 declare: Whether to exchange automatically or just connect to it.
94 arguments: Exchange declarationg arguments.
95 timeout: Send confirmation time from RabbitMQ.
96 robust: Whether to declare exchange object as restorable.
97 bind_to: Another `RabbitExchange` object to bind the current one to.
98 bind_arguments: Exchange-exchange binding options.
99 routing_key: Explicit binding routing key.
100 """
101 if routing_key and bind_to is None: # pragma: no cover
102 warnings.warn(
103 (
104 "\nRabbitExchange `routing_key` is using to bind exchange to another one."
105 "\nIt can be used only with the `bind_to` argument, please setup it too."
106 ),
107 category=RuntimeWarning,
108 stacklevel=1,
109 )
111 super().__init__(name)
113 self.type = type
114 self.durable = durable
115 self.auto_delete = auto_delete
116 self.robust = robust
117 self.timeout = timeout
118 self.arguments = arguments
119 self.declare = declare
120 self.bind_to = bind_to
121 self.bind_arguments = bind_arguments
122 self.routing_key = routing_key
124 @override
125 @classmethod
126 def validate( # type: ignore[override]
127 cls,
128 value: Union[str, "RabbitExchange", None],
129 **kwargs: Any,
130 ) -> "RabbitExchange":
131 exch = super().validate(value, **kwargs)
132 if exch is None:
133 exch = RabbitExchange()
134 return exch