fix: raise ChordFormatError when detokenize produces empty bars
A sequence of only metadata tokens followed by EOS would silently return a ChordPeriod with bars=[], which would later crash or produce an empty .chord file. Now raises immediately with a descriptive message. Added a failing-then- passing test to cover this path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -508,6 +508,12 @@ def detokenize_to_period(token_ids: list[int]) -> ChordPeriod:
|
|||||||
pos_in_bar, positions_per_bar,
|
pos_in_bar, positions_per_bar,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not bars:
|
||||||
|
raise ChordFormatError(
|
||||||
|
"token sequence produced no complete bars "
|
||||||
|
f"(last partial bar had {pos_in_bar}/{positions_per_bar} positions)"
|
||||||
|
)
|
||||||
|
|
||||||
return ChordPeriod(
|
return ChordPeriod(
|
||||||
title="detokenized",
|
title="detokenized",
|
||||||
key=key,
|
key=key,
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from src.tokenizer import (
|
|||||||
ID_TO_TOKEN,
|
ID_TO_TOKEN,
|
||||||
TOKEN_TO_ID,
|
TOKEN_TO_ID,
|
||||||
VOCAB,
|
VOCAB,
|
||||||
|
ChordFormatError,
|
||||||
ChordPeriod,
|
ChordPeriod,
|
||||||
detokenize_to_period,
|
detokenize_to_period,
|
||||||
parse_chord_file,
|
parse_chord_file,
|
||||||
@@ -206,3 +207,17 @@ class TestRoundTrip:
|
|||||||
t = parse_chord_symbol(recovered.bars[0][0])
|
t = parse_chord_symbol(recovered.bars[0][0])
|
||||||
assert t.root == "A"
|
assert t.root == "A"
|
||||||
assert t.quality == "m"
|
assert t.quality == "m"
|
||||||
|
|
||||||
|
def test_empty_bar_sequence_raises(self):
|
||||||
|
# BOS + metadata only, then EOS — no body tokens → must raise
|
||||||
|
ids = [
|
||||||
|
TOKEN_TO_ID["<BOS>"],
|
||||||
|
TOKEN_TO_ID["MODE_major"],
|
||||||
|
TOKEN_TO_ID["TIME_4/4"],
|
||||||
|
TOKEN_TO_ID["SUB_4"],
|
||||||
|
TOKEN_TO_ID["STYLE_H1K0"],
|
||||||
|
TOKEN_TO_ID["FUNC_chorus"],
|
||||||
|
TOKEN_TO_ID["<EOS>"],
|
||||||
|
]
|
||||||
|
with pytest.raises(ChordFormatError, match="no complete bars"):
|
||||||
|
detokenize_to_period(ids)
|
||||||
|
|||||||
Reference in New Issue
Block a user