Coverage for faststream/_internal/kafka/keys.py: 88%

14 statements  

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

1"""Per-message keys for batch publishing. 

2 

3A Kafka batch may carry a distinct key per message, so the keys have to be 

4extracted from the bodies and kept aligned with them whenever the batch is 

5rewritten. 

6""" 

7 

8from collections.abc import Sequence 

9from functools import singledispatch 

10from typing import Any 

11 

12from faststream.response.response import Response 

13 

14 

15@singledispatch 

16def _extract_body_and_key(item: Any) -> tuple[Any, Any | None]: 

17 """Extract body and key from a plain message. 

18 

19 Default implementation for non-Response objects. 

20 Returns the item as-is for body and None for key. 

21 """ 

22 return item, None 

23 

24 

25@_extract_body_and_key.register 

26def _(item: Response) -> tuple[Any, Any | None]: 

27 """Extract body and key from a Response object. 

28 

29 Uses polymorphic get_publish_key() method to retrieve the key. 

30 """ 

31 return item.body, item.get_publish_key() 

32 

33 

34def extract_per_message_keys_and_bodies( 

35 batch_bodies: Sequence[Any], 

36) -> tuple[tuple[Any | None, ...], tuple[Any, ...] | None]: 

37 """Extract per-message keys and optionally normalized bodies from a batch. 

38 

39 Returns a pair (keys, normalized_bodies_or_None): 

40 - If no Response objects are present, returns ((), None) 

41 so callers can reuse the original bodies without extra allocations. 

42 - Otherwise returns (keys_tuple, normalized_bodies_tuple), where normalized bodies 

43 contain the extracted 'body' values from Response objects (or the original item). 

44 

45 Supports passing Response objects (e.g., KafkaResponse) to set per-message keys: 

46 await broker.publish_batch( 

47 KafkaResponse("body1", key=b"key1"), 

48 KafkaResponse("body2", key=b"key2"), 

49 "plain message" # uses default key 

50 ) 

51 

52 Uses singledispatch for type-based polymorphism without isinstance checks. 

53 """ 

54 if not batch_bodies: 

55 return (), None 

56 

57 bodies: list[Any] = [] 

58 keys: list[Any | None] = [] 

59 has_key: bool = False 

60 

61 for item in batch_bodies: 

62 body, key = _extract_body_and_key(item) 

63 bodies.append(body) 

64 keys.append(key) 

65 if key is not None: 

66 has_key = True 

67 

68 if not has_key: 

69 return (), None 

70 

71 return tuple(keys), tuple(bodies) 

72 

73 

74def key_for_index( 

75 keys: Sequence[Any | None], default_key: Any | None, index: int 

76) -> Any | None: 

77 """Return the effective key for a given message index. 

78 

79 Prefers a per-message key at the given index when it is not None; 

80 otherwise falls back to ``default_key``. If the index is out of bounds 

81 or negative, ``default_key`` is returned. 

82 """ 

83 if index < 0: 83 ↛ 84line 83 didn't jump to line 84 because the condition on line 83 was never true

84 return default_key 

85 

86 try: 

87 k = keys[index] 

88 except IndexError: 

89 return default_key 

90 

91 return k if k is not None else default_key 

92 

93 

94def realign_keys( 

95 keys: Sequence[Any | None], 

96 current_bodies: Sequence[Any], 

97 new_bodies: Sequence[Any], 

98) -> tuple[Any | None, ...]: 

99 """Realign per-message keys with a new batch of bodies.""" 

100 bodies_seen: dict[int, Any] = {} 

101 for body in new_bodies: 

102 index = current_bodies.index(body) 

103 if bodies_seen.get(index) is None: 

104 bodies_seen.update({index: body}) 

105 continue 

106 while bodies_seen.get(index) is not None or current_bodies[index] != body: 

107 index += 1 

108 bodies_seen.update({index: body}) 

109 return tuple(keys[i] for i in bodies_seen)