From 2c0af9017cf1c35eca2acad2613f75b079cc18af Mon Sep 17 00:00:00 2001 From: minyoungci Date: Sat, 17 Oct 2026 21:56:54 +0900 Subject: [PATCH] Return empty offsets for empty batches --- src/semchunk/semchunk.py | 3 +++ tests/test_empty_batch.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/test_empty_batch.py diff --git a/src/semchunk/semchunk.py b/src/semchunk/semchunk.py index 6538383..4a1bade 100644 --- a/src/semchunk/semchunk.py +++ b/src/semchunk/semchunk.py @@ -761,6 +761,9 @@ def __call__( chunks_and_offsets = list(chunked_texts) if offsets: + if not chunks_and_offsets: + return [], [] + chunks, offsets_ = zip(*chunks_and_offsets) return list(chunks), list(offsets_) diff --git a/tests/test_empty_batch.py b/tests/test_empty_batch.py new file mode 100644 index 0000000..e980e4f --- /dev/null +++ b/tests/test_empty_batch.py @@ -0,0 +1,33 @@ +"""Check empty batch results and the neighboring input shapes.""" + +import pytest + +import semchunk + + +@pytest.mark.parametrize("texts", [[], ()], ids=["list", "tuple"]) +@pytest.mark.parametrize("offsets", [False, True]) +@pytest.mark.parametrize("progress", [False, True]) +@pytest.mark.parametrize("processes", [1, 2]) +def test_empty_batch(texts, offsets, progress, processes): + chunker = semchunk.chunkerify(len, 4, memoize=False) + + assert chunker(texts, offsets=offsets, progress=progress, processes=processes) == (([], []) if offsets else []) + + +@pytest.mark.parametrize("offsets", [False, True]) +@pytest.mark.parametrize("progress", [False, True]) +@pytest.mark.parametrize("processes", [1, 2]) +def test_nonempty_batch(offsets, progress, processes): + chunker = semchunk.chunkerify(len, 4, memoize=False) + chunks = [["abcd", "ef"], []] + expected = (chunks, [[(0, 4), (5, 7)], []]) if offsets else chunks + + assert chunker(("abcd ef", ""), offsets=offsets, progress=progress, processes=processes) == expected + + +@pytest.mark.parametrize("offsets", [False, True]) +def test_empty_string(offsets): + chunker = semchunk.chunkerify(len, 4, memoize=False) + + assert chunker("", offsets=offsets) == (([], []) if offsets else [])