60
60
from dataclasses import dataclass
61
61
from typing import Any , final
62
62
63
- from typing_extensions import Self , TypeIs , assert_never
63
+ from typing_extensions import Self
64
64
65
65
import aiokafka .codec as codecs
66
66
from aiokafka .codec import (
82
82
DefaultRecordMetadataProtocol ,
83
83
DefaultRecordProtocol ,
84
84
)
85
- from ._types import (
86
- CodecGzipT ,
87
- CodecLz4T ,
88
- CodecMaskT ,
89
- CodecNoneT ,
90
- CodecSnappyT ,
91
- CodecZstdT ,
92
- )
93
85
from .util import calc_crc32c , decode_varint , encode_varint , size_of_varint
94
86
95
87
@@ -116,12 +108,12 @@ class DefaultRecordBase:
116
108
CRC_OFFSET = struct .calcsize (">qiib" )
117
109
AFTER_LEN_OFFSET = struct .calcsize (">qi" )
118
110
119
- CODEC_MASK : CodecMaskT = 0x07
120
- CODEC_NONE : CodecNoneT = 0x00
121
- CODEC_GZIP : CodecGzipT = 0x01
122
- CODEC_SNAPPY : CodecSnappyT = 0x02
123
- CODEC_LZ4 : CodecLz4T = 0x03
124
- CODEC_ZSTD : CodecZstdT = 0x04
111
+ CODEC_MASK = 0x07
112
+ CODEC_NONE = 0x00
113
+ CODEC_GZIP = 0x01
114
+ CODEC_SNAPPY = 0x02
115
+ CODEC_LZ4 = 0x03
116
+ CODEC_ZSTD = 0x04
125
117
TIMESTAMP_TYPE_MASK = 0x08
126
118
TRANSACTIONAL_MASK = 0x10
127
119
CONTROL_MASK = 0x20
@@ -131,9 +123,7 @@ class DefaultRecordBase:
131
123
132
124
NO_PARTITION_LEADER_EPOCH = - 1
133
125
134
- def _assert_has_codec (
135
- self , compression_type : int
136
- ) -> TypeIs [CodecGzipT | CodecSnappyT | CodecLz4T | CodecZstdT ]:
126
+ def _assert_has_codec (self , compression_type : int ) -> bool :
137
127
if compression_type == self .CODEC_GZIP :
138
128
checker , name = codecs .has_gzip , "gzip"
139
129
elif compression_type == self .CODEC_SNAPPY :
@@ -240,7 +230,10 @@ def _maybe_uncompress(self) -> None:
240
230
elif compression_type == self .CODEC_ZSTD :
241
231
uncompressed = zstd_decode (data .tobytes ())
242
232
else :
243
- assert_never (compression_type )
233
+ # Must not be possible
234
+ raise RuntimeError (
235
+ f"Invalid compression codec { compression_type :#04x} "
236
+ )
244
237
self ._buffer = bytearray (uncompressed )
245
238
self ._pos = 0
246
239
self ._decompressed = True
@@ -560,7 +553,10 @@ def _maybe_compress(self) -> bool:
560
553
elif self ._compression_type == self .CODEC_ZSTD :
561
554
compressed = zstd_encode (data )
562
555
else :
563
- assert_never (self ._compression_type )
556
+ # Must not be possible
557
+ raise RuntimeError (
558
+ f"Invalid compression codec { self ._compression_type :#04x} "
559
+ )
564
560
compressed_size = len (compressed )
565
561
if len (data ) <= compressed_size :
566
562
# We did not get any benefit from compression, lets send
0 commit comments