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

1from abc import abstractmethod 

2from typing import TYPE_CHECKING, Any, Protocol 

3 

4from faststream._internal.types import PublishCommandType_contra 

5from faststream.exceptions import IncorrectState 

6 

7if TYPE_CHECKING: 

8 from faststream._internal.parser import CodecProto 

9 from faststream._internal.types import AsyncCallable 

10 from faststream.response import PublishCommand 

11 

12 

13class ProducerProto(Protocol[PublishCommandType_contra]): 

14 __slots__ = () 

15 

16 _parser: "AsyncCallable" 

17 _decoder: "AsyncCallable" 

18 codec: "CodecProto" 

19 

20 @abstractmethod 

21 async def publish(self, cmd: "PublishCommandType_contra") -> Any: 

22 """Publishes a message asynchronously.""" 

23 ... 

24 

25 @abstractmethod 

26 async def request(self, cmd: "PublishCommandType_contra") -> Any: 

27 """Publishes a message synchronously.""" 

28 ... 

29 

30 @abstractmethod 

31 async def publish_batch(self, cmd: "PublishCommandType_contra") -> Any: 

32 """Publishes a messages batch asynchronously.""" 

33 ... 

34 

35 

36class ProducerFactory(Protocol): 

37 __slots__ = () 

38 

39 def __call__( 

40 self, 

41 parser: "AsyncCallable", 

42 decoder: "AsyncCallable", 

43 ) -> ProducerProto: ... 

44 

45 

46class ProducerUnset(ProducerProto): 

47 __slots__ = () 

48 

49 msg = "Producer is unset yet. You should set producer in broker initial method." 

50 

51 def __bool__(self) -> bool: 

52 return False 

53 

54 @property 

55 def _parser(self) -> "AsyncCallable": 

56 raise IncorrectState(self.msg) 

57 

58 @_parser.setter 

59 def _parser(self, value: "AsyncCallable", /) -> "AsyncCallable": 

60 raise IncorrectState(self.msg) 

61 

62 @property 

63 def _decoder(self) -> "AsyncCallable": 

64 raise IncorrectState(self.msg) 

65 

66 @_decoder.setter 

67 def _decoder(self, value: "AsyncCallable", /) -> "AsyncCallable": 

68 raise IncorrectState(self.msg) 

69 

70 @property 

71 def codec(self) -> "CodecProto": 

72 raise IncorrectState(self.msg) 

73 

74 @codec.setter 

75 def codec(self, value: "CodecProto", /) -> None: 

76 raise IncorrectState(self.msg) 

77 

78 async def publish(self, cmd: "PublishCommand") -> Any | None: 

79 raise IncorrectState(self.msg) 

80 

81 async def request(self, cmd: "PublishCommand") -> Any: 

82 raise IncorrectState(self.msg) 

83 

84 async def publish_batch(self, cmd: "PublishCommand") -> None: 

85 raise IncorrectState(self.msg)