Coverage for faststream/confluent/schemas/partition.py: 82%

18 statements  

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

1from typing import TYPE_CHECKING 

2 

3from confluent_kafka import TopicPartition as ConfluentPartition 

4 

5if TYPE_CHECKING: 

6 from typing_extensions import NotRequired, TypedDict 

7 

8 class _TopicKwargs(TypedDict): 

9 topic: str 

10 partition: int 

11 offset: int 

12 metadata: NotRequired[str] 

13 leader_epoch: NotRequired[int] 

14 

15 

16class TopicPartition: 

17 __slots__ = ( 

18 "declare", 

19 "leader_epoch", 

20 "metadata", 

21 "offset", 

22 "partition", 

23 "topic", 

24 ) 

25 

26 def __init__( 

27 self, 

28 topic: str, 

29 partition: int = -1, 

30 offset: int = -1001, 

31 metadata: str | None = None, 

32 leader_epoch: int | None = None, 

33 *, 

34 declare: bool = True, 

35 ) -> None: 

36 """Initialize the Kafka topic partition. 

37 

38 Args: 

39 topic: Kafka topic name. 

40 partition: Partition number to assign. 

41 offset: Offset to start consuming from. 

42 metadata: Application-specific metadata to attach to the partition. 

43 leader_epoch: Leader epoch of the offset. 

44 declare: Whether to create the topic automatically or just connect to it. 

45 Missing topics are not created and their absence is not reported, 

46 so set it to `False` for topics provisioned by someone else. 

47 """ 

48 self.topic = topic 

49 self.partition = partition 

50 self.offset = offset 

51 self.metadata = metadata 

52 self.leader_epoch = leader_epoch 

53 self.declare = declare 

54 

55 def to_confluent(self) -> ConfluentPartition: 

56 kwargs: _TopicKwargs = { 

57 "topic": self.topic, 

58 "partition": self.partition, 

59 "offset": self.offset, 

60 } 

61 if self.metadata is not None: 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true

62 kwargs["metadata"] = self.metadata 

63 if self.leader_epoch is not None: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 kwargs["leader_epoch"] = self.leader_epoch 

65 return ConfluentPartition(**kwargs) 

66 

67 def add_prefix(self, prefix: str) -> "TopicPartition": 

68 return TopicPartition( 

69 topic=f"{prefix}{self.topic}", 

70 partition=self.partition, 

71 offset=self.offset, 

72 metadata=self.metadata, 

73 leader_epoch=self.leader_epoch, 

74 declare=self.declare, 

75 )