feat(compression): reject empty compression spec (#3678)

An empty spec list passed to compress() previously returned an
unmodified model silently. Fail early with a clear error instead,
since an empty spec is almost certainly a mistake.

BUG=part of #3256
This commit is contained in:
Ryan Kuester 2026-08-24 23:11:41 +00:00 committed by GitHub
parent 733736087d
commit 13cd6c1550
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View file

@ -132,6 +132,11 @@ def compress(model_in: ByteString, specs: Iterable[spec.Tensor]) -> bytearray:
Returns:
A compressed flatbuffer with DECODE operators inserted.
"""
specs = list(specs)
if not specs:
raise compressor.CompressionError(
"Compression spec is empty; no tensors to compress")
model = model_editor.read(model_in)
compression_results: dict[tuple[int, int], compressor.CompressionResult] = {}

View file

@ -313,6 +313,11 @@ class TestCompression(unittest.TestCase):
self.assertEqual(dcm_bytes[5] & 0x07, 4) # bitwidth = 4
self.assertEqual(dcm_bytes[6], 4) # stride = num unique values
def test_empty_spec_raises(self):
"""Empty compression spec is an error, not a silent no-op."""
self.assertRaisesRegex(compressor.CompressionError, "empty",
lambda: compress.compress(self.flatbuffer, []))
def test_smaller_bitwidth_raises(self):
"""Specifying LUT compression with too small a bitwidth fails."""
specs = [