refactor(whisper_cpp): extract methods from transcribe to fix too-many-statements#1540
Merged
raivisdejus merged 17 commits intoJul 1, 2026
Conversation
Decompose ModelDownloader.download_model (142 statements, > 50 limit) into four focused helpers: _prepare_resume_download - existing file validation & resume logic _check_range_support - server Range-request capability _stream_download - HTTP streaming with progress/cancel _verify_sha256 - post-download integrity check download_model now acts as a high-level orchestrator (~16 statements).
…many-statements (R0915)
…statements (R0915) Extraiu 7 métodos auxiliares e 3 funções modulares do método transcribe (166 statements, limite 50), reduzindo-o para ~30 statements. Métodos extraídos para WhisperCpp: - _convert_to_wav, _build_command, _run_whisper - _read_json_output, _parse_word_level_timings - _parse_segment_timings, _cleanup_files Funções extraídas para o módulo: - _make_offset_mapper, _flush_complete_chars, _append_word
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1540 +/- ##
==========================================
+ Coverage 82.88% 82.91% +0.03%
==========================================
Files 108 108
Lines 11653 11668 +15
==========================================
+ Hits 9659 9675 +16
+ Misses 1994 1993 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why is this change needed?
The
WhisperCpp.transcribemethod had 166 statements, far exceeding Pylint's default limit of 50 (too-many-statements/ R0915). This made the method difficult to read, test, and maintain, as it combined multiple responsibilities in a single monolithic block:What was done?
The method was decomposed into smaller, cohesive units:
Module-level functions (extracted from nested inner functions):
_make_offset_mapper— maps VAD-compressed token offsets back to original audio timeline_flush_complete_chars— decodes multi-token UTF-8 characters (for CJK languages)_append_word— decodes space-separated word segmentsStatic methods on
WhisperCpp:_convert_to_wav— handles FFmpeg conversion from unsupported formats_build_command— assembles the whisper-cli command line_run_whisper— runs the subprocess and forwards stderr output_read_json_output— reads the generated JSON file_parse_word_level_timings— parses word-level timestamps from JSON_parse_segment_timings— parses segment-level timestamps from JSON_cleanup_files— removes temporary filesThe
transcribemethod now acts as an orchestrator (~30 statements), delegating each step to these helpers.How was it tested?
All 10 existing tests (both integration and mocked unit tests) pass:
tests/transcriber/whisper_cpp_test.py ............. [100%] ======================= 10 passed in 4.97s =======================
Tests were run with
uv run pytest tests/transcriber/whisper_cpp_test.py -v. No functionality was changed — only code was rearranged.Notes for the reviewer
_make_offset_mapper,_flush_complete_chars,_append_word) were previously defined as nested inner functions insidetranscribe. They were lifted to module scope to reduce the method's statement count. Thesegmentslist is now passed as an explicit parameter instead of being captured via closure._run_whispermethod intentionally drops thestderr_outputlist that was previously accumulated but never used outside the loop."Failed to remove JSON output file"warnings in stderr — this is the same behavior as before (the exception is caught and logged).