Skip to content

Fix sorting of equal-time subtitles with missing indexes - #104

Open
shkyyy18 wants to merge 1 commit into
cdown:developfrom
shkyyy18:fix/sort-missing-indices
Open

shkyyy18 wants to merge 1 commit into
cdown:developfrom
shkyyy18:fix/sort-missing-indices

Conversation

@shkyyy18

@shkyyy18 shkyyy18 commented Oct 1, 2026

Copy link
Copy Markdown

Bug

Subtitle.index supports None, and parse() accepts missing indexes. However, when two subtitles have the same start and end times, Subtitle.__lt__ directly compares their indexes. Mixing an indexed and unindexed subtitle raises TypeError in sort_and_reindex() and the default compose() path.

For example:

import srt
text = (
    "00:00:01,000 --> 00:00:02,000\nUnindexed\n\n"
    "1\n00:00:01,000 --> 00:00:02,000\nIndexed\n\n"
)
srt.compose(srt.parse(text))

Fix

Normalize missing indexes to zero in the comparison key, matching to_srt()'s existing fallback. Preserve numeric index ordering and stable order for equal fallback keys; do not mutate the input subtitles.

Validation

  • Added nine synthetic regression/control cases covering both input orders, negative/zero/absent indexes, and parse/compose round trips.
  • Before: eight new cases raise TypeError; the all-None control passes.
  • After: python -m pytest -q --doctest-modules — 60 passed on Python 3.12.10 (pytest 9.1.1, Hypothesis 6.168.3). Original tests alone passed on the unmodified baseline (45).
  • Black 26.5.1 accepts the test file. Its check on srt.py reports one pre-existing extra blank line near the imports, identically on the unmodified baseline; left that unrelated line untouched.
  • The full tox / older Python matrix was not run locally.

AI assistance was used to investigate, implement, and test this change. All examples are synthetic; no private subtitle data is included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant