Coverage for faststream/redis/configs/broker.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from dataclasses import dataclass, field 

2from types import MappingProxyType 

3from typing import TYPE_CHECKING 

4 

5from faststream._internal.configs import ( 

6 BrokerConfig, 

7 UnderlyingDriverAnnotation, 

8 UnderlyingDriverAnnotations, 

9) 

10from faststream._internal.parser import DefaultCodec 

11from faststream.exceptions import IncorrectState 

12 

13if TYPE_CHECKING: 

14 from redis.asyncio.client import Redis 

15 from redis.asyncio.cluster import RedisCluster 

16 

17 from faststream.redis.parser import MessageFormat 

18 from faststream.redis.publisher.producer import ( 

19 RedisClusterFastProducer, 

20 RedisFastProducer, 

21 ) 

22 

23 from .state import ConnectionState 

24 

25 

26def _context_annotations_factory() -> UnderlyingDriverAnnotations: 

27 # `annotations` reaches this module through the broker, so the 

28 # objects a row needs only exist once the package is built. 

29 from redis.asyncio.client import ( # ruff: ignore[import-outside-top-level] 

30 Pipeline as PipelineDriver, 

31 Redis as RedisDriver, 

32 ) 

33 

34 from faststream.redis import annotations # ruff: ignore[import-outside-top-level] 

35 from faststream.redis.broker.broker import ( # ruff: ignore[import-outside-top-level] 

36 RedisBroker as RedisBrokerDriver, 

37 ) 

38 from faststream.redis.message import ( # ruff: ignore[import-outside-top-level] 

39 RedisBatchStreamMessage as RedisBatchStreamMessageDriver, 

40 RedisChannelMessage as RedisChannelMessageDriver, 

41 RedisListMessage as RedisListMessageDriver, 

42 RedisMessage as RedisMessageDriver, 

43 RedisStreamMessage as RedisStreamMessageDriver, 

44 ) 

45 

46 return MappingProxyType( 

47 { 

48 RedisDriver: UnderlyingDriverAnnotation( 

49 type_hint=annotations.Redis, 

50 module="faststream.redis.annotations", 

51 name="Redis", 

52 ), 

53 PipelineDriver: UnderlyingDriverAnnotation( 

54 type_hint=annotations.Pipeline, 

55 module="faststream.redis.annotations", 

56 name="Pipeline", 

57 ), 

58 RedisBrokerDriver: UnderlyingDriverAnnotation( 

59 type_hint=annotations.RedisBroker, 

60 module="faststream.redis.annotations", 

61 name="RedisBroker", 

62 ), 

63 RedisMessageDriver: UnderlyingDriverAnnotation( 

64 type_hint=annotations.RedisMessage, 

65 module="faststream.redis.annotations", 

66 name="RedisMessage", 

67 ), 

68 RedisChannelMessageDriver: UnderlyingDriverAnnotation( 

69 type_hint=annotations.RedisChannelMessage, 

70 module="faststream.redis.annotations", 

71 name="RedisChannelMessage", 

72 ), 

73 RedisStreamMessageDriver: UnderlyingDriverAnnotation( 

74 type_hint=annotations.RedisStreamMessage, 

75 module="faststream.redis.annotations", 

76 name="RedisStreamMessage", 

77 ), 

78 RedisBatchStreamMessageDriver: UnderlyingDriverAnnotation( 

79 type_hint=annotations.RedisBatchStreamMessage, 

80 module="faststream.redis.annotations", 

81 name="RedisBatchStreamMessage", 

82 ), 

83 RedisListMessageDriver: UnderlyingDriverAnnotation( 

84 type_hint=annotations.RedisListMessage, 

85 module="faststream.redis.annotations", 

86 name="RedisListMessage", 

87 ), 

88 }, 

89 ) 

90 

91 

92@dataclass(kw_only=True) 

93class RedisBrokerConfig(BrokerConfig): 

94 producer: "RedisFastProducer | RedisClusterFastProducer" 

95 connection: "ConnectionState[Redis[bytes]] | ConnectionState[RedisCluster[bytes]]" 

96 

97 message_format: type["MessageFormat"] 

98 

99 default_driver_annotations: UnderlyingDriverAnnotations = field( 

100 default_factory=_context_annotations_factory, 

101 ) 

102 

103 async def connect(self) -> None: 

104 self.producer.connect( 

105 self.fd_config._serializer, codec=self.broker_codec or DefaultCodec() 

106 ) 

107 await self.connection.connect() 

108 

109 async def disconnect(self) -> None: 

110 await self.connection.disconnect() 

111 

112 

113@dataclass(kw_only=True) 

114class RedisRouterConfig(BrokerConfig): 

115 default_driver_annotations: UnderlyingDriverAnnotations = field( 

116 default_factory=_context_annotations_factory, 

117 ) 

118 

119 @property 

120 def connection(self) -> ConnectionError: 

121 raise IncorrectState