Coverage for faststream/_internal/producer.py: 72%
36 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
1from abc import abstractmethod
2from typing import TYPE_CHECKING, Any, Protocol
4from faststream._internal.types import PublishCommandType_contra
5from faststream.exceptions import IncorrectState
7if TYPE_CHECKING:
8 from faststream._internal.parser import CodecProto
9 from faststream._internal.types import AsyncCallable
10 from faststream.response import PublishCommand
13class ProducerProto(Protocol[PublishCommandType_contra]):
14 __slots__ = ()
16 _parser: "AsyncCallable"
17 _decoder: "AsyncCallable"
18 codec: "CodecProto"
20 @abstractmethod
21 async def publish(self, cmd: "PublishCommandType_contra") -> Any:
22 """Publishes a message asynchronously."""
23 ...
25 @abstractmethod
26 async def request(self, cmd: "PublishCommandType_contra") -> Any:
27 """Publishes a message synchronously."""
28 ...
30 @abstractmethod
31 async def publish_batch(self, cmd: "PublishCommandType_contra") -> Any:
32 """Publishes a messages batch asynchronously."""
33 ...
36class ProducerFactory(Protocol):
37 __slots__ = ()
39 def __call__(
40 self,
41 parser: "AsyncCallable",
42 decoder: "AsyncCallable",
43 ) -> ProducerProto: ...
46class ProducerUnset(ProducerProto):
47 __slots__ = ()
49 msg = "Producer is unset yet. You should set producer in broker initial method."
51 def __bool__(self) -> bool:
52 return False
54 @property
55 def _parser(self) -> "AsyncCallable":
56 raise IncorrectState(self.msg)
58 @_parser.setter
59 def _parser(self, value: "AsyncCallable", /) -> "AsyncCallable":
60 raise IncorrectState(self.msg)
62 @property
63 def _decoder(self) -> "AsyncCallable":
64 raise IncorrectState(self.msg)
66 @_decoder.setter
67 def _decoder(self, value: "AsyncCallable", /) -> "AsyncCallable":
68 raise IncorrectState(self.msg)
70 @property
71 def codec(self) -> "CodecProto":
72 raise IncorrectState(self.msg)
74 @codec.setter
75 def codec(self, value: "CodecProto", /) -> None:
76 raise IncorrectState(self.msg)
78 async def publish(self, cmd: "PublishCommand") -> Any | None:
79 raise IncorrectState(self.msg)
81 async def request(self, cmd: "PublishCommand") -> Any:
82 raise IncorrectState(self.msg)
84 async def publish_batch(self, cmd: "PublishCommand") -> None:
85 raise IncorrectState(self.msg)