Skip to content

Fix #2839: initialize punc_res before conditional to prevent UnboundLocalError#2840

Open
Lidang-Jiang wants to merge 1 commit intomodelscope:mainfrom
Lidang-Jiang:fix/issue-2839-punc-model-unbound
Open

Fix #2839: initialize punc_res before conditional to prevent UnboundLocalError#2840
Lidang-Jiang wants to merge 1 commit intomodelscope:mainfrom
Lidang-Jiang:fix/issue-2839-punc-model-unbound

Conversation

@Lidang-Jiang
Copy link
Copy Markdown

Summary

Fix UnboundLocalError when punc_model is None or not provided in AutoModel.generate() with VAD enabled.

Root cause: In inference_with_vad(), punc_res is only assigned inside the if self.punc_model is not None: block, but downstream code paths (punc_segment speaker diarization and sentence_timestamp) access it unconditionally.

Fix:

  • Initialize punc_res = None before the conditional block
  • Add punc_res is None guards with appropriate log messages in the two affected code paths
Before (UnboundLocalError on unpatched code)
$ python -m pytest tests/test_punc_model_none.py -v

tests/test_punc_model_none.py::TestPuncModelNone::test_punc_model_none_basic PASSED [ 33%]
tests/test_punc_model_none.py::TestPuncModelNone::test_punc_model_with_value_still_works PASSED [ 66%]
tests/test_punc_model_none.py::TestPuncModelNone::test_sentence_timestamp_with_punc_model_none FAILED [100%]

________ TestPuncModelNone.test_sentence_timestamp_with_punc_model_none ________

E                       UnboundLocalError: cannot access local variable 'punc_res' where it is not associated with a value

funasr/auto/auto_model.py:677: UnboundLocalError

=========================== short test summary info ============================
FAILED tests/test_punc_model_none.py::TestPuncModelNone::test_sentence_timestamp_with_punc_model_none
======================== 1 failed, 2 passed in 4.10s ==========================
After (all tests pass)
$ python -m pytest tests/test_punc_model_none.py -v

============================= test session starts ==============================
platform linux -- Python 3.12.12, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/devuser/workspace/FunASR-2839
plugins: hydra-core-1.3.2
collecting ... collected 3 items

tests/test_punc_model_none.py::TestPuncModelNone::test_punc_model_none_basic PASSED [ 33%]
tests/test_punc_model_none.py::TestPuncModelNone::test_punc_model_with_value_still_works PASSED [ 66%]
tests/test_punc_model_none.py::TestPuncModelNone::test_sentence_timestamp_with_punc_model_none PASSED [100%]

============================== 3 passed in 3.04s ===============================

Test plan

  • test_punc_model_none_basic — basic ASR with punc_model=None returns correct text
  • test_sentence_timestamp_with_punc_model_nonesentence_timestamp=True with punc_model=None returns empty sentence_info instead of crashing
  • test_punc_model_with_value_still_works — normal flow with punc_model provided still produces punctuated text

🤖 Generated with Claude Code

…t UnboundLocalError

When punc_model is None or not provided, punc_res was only assigned inside
the `if self.punc_model is not None:` block. Downstream code paths
(punc_segment speaker diarization and sentence_timestamp) accessed punc_res
unconditionally, causing UnboundLocalError.

Changes:
- Initialize punc_res = None before the conditional block
- Add punc_res is None guard in punc_segment path with error log
- Add punc_res is None guard in sentence_timestamp path with warning log
- Add unit tests covering punc_model=None, empty punc_model, and normal flow

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an UnboundLocalError in the inference_with_vad method by ensuring punc_res is properly initialized and checked before use when punc_model is None. The changes also include code formatting improvements and a new test suite to verify the fix. Feedback suggests refining the docstring in the new test file to accurately reflect that only the punc_model=None case is tested, as empty strings are not covered and may lead to different initialization issues.

@@ -0,0 +1,119 @@
"""Tests for issue #2839: punc_model=None or empty string should not cause UnboundLocalError."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The docstring states that these tests cover punc_model=None or an empty string. However, the tests only cover the None case. An empty string for punc_model would likely cause a different failure mode during model initialization, not the UnboundLocalError this PR aims to fix. To avoid confusion, please update the docstring to only mention the punc_model=None case.

Suggested change
"""Tests for issue #2839: punc_model=None or empty string should not cause UnboundLocalError."""
"""Tests for issue #2839: punc_model=None should not cause UnboundLocalError."""

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