mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-19 16:12:30 +00:00
fix(transcription): address review nits on PR #3253
- Correct api_key type hint to str | None in _post_transcription_with_retry - Remove unreachable final return "" - Fix test_openai_missing_api_key_short_circuits to actually test missing-key path (use audio_file fixture so file exists) - Fix PermissionError patch for Windows (patch class method instead of instance attribute)
This commit is contained in:
parent
7ebf611be8
commit
3437ff273f
@ -26,7 +26,7 @@ _RETRYABLE_EXCEPTIONS = (
|
|||||||
async def _post_transcription_with_retry(
|
async def _post_transcription_with_retry(
|
||||||
url: str,
|
url: str,
|
||||||
*,
|
*,
|
||||||
api_key: str,
|
api_key: str | None,
|
||||||
path: Path,
|
path: Path,
|
||||||
model: str,
|
model: str,
|
||||||
provider_label: str,
|
provider_label: str,
|
||||||
@ -116,8 +116,6 @@ async def _post_transcription_with_retry(
|
|||||||
return ""
|
return ""
|
||||||
return payload.get("text", "")
|
return payload.get("text", "")
|
||||||
|
|
||||||
return ""
|
|
||||||
|
|
||||||
|
|
||||||
class OpenAITranscriptionProvider:
|
class OpenAITranscriptionProvider:
|
||||||
"""Voice transcription provider using OpenAI's Whisper API."""
|
"""Voice transcription provider using OpenAI's Whisper API."""
|
||||||
|
|||||||
@ -134,14 +134,13 @@ async def test_groq_does_not_retry_on_auth_error(audio_file: Path) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_openai_missing_api_key_short_circuits(tmp_path: Path) -> None:
|
async def test_openai_missing_api_key_short_circuits(audio_file: Path) -> None:
|
||||||
provider = OpenAITranscriptionProvider(api_key=None)
|
"""Missing API key short-circuits before any HTTP call, even when the file exists."""
|
||||||
# Ensure env var doesn't accidentally satisfy it.
|
|
||||||
with patch.dict("os.environ", {}, clear=True):
|
with patch.dict("os.environ", {}, clear=True):
|
||||||
provider = OpenAITranscriptionProvider(api_key=None)
|
provider = OpenAITranscriptionProvider(api_key=None)
|
||||||
post = AsyncMock()
|
post = AsyncMock()
|
||||||
with patch("httpx.AsyncClient.post", post):
|
with patch("httpx.AsyncClient.post", post):
|
||||||
assert await provider.transcribe(tmp_path / "voice.ogg") == ""
|
assert await provider.transcribe(audio_file) == ""
|
||||||
assert post.await_count == 0
|
assert post.await_count == 0
|
||||||
|
|
||||||
|
|
||||||
@ -159,7 +158,7 @@ async def test_returns_empty_when_file_unreadable(audio_file: Path) -> None:
|
|||||||
"""Existing file that cannot be read (PermissionError/OSError): "" with no HTTP attempt."""
|
"""Existing file that cannot be read (PermissionError/OSError): "" with no HTTP attempt."""
|
||||||
provider = OpenAITranscriptionProvider(api_key="sk-test")
|
provider = OpenAITranscriptionProvider(api_key="sk-test")
|
||||||
post = AsyncMock()
|
post = AsyncMock()
|
||||||
with patch.object(Path, "read_bytes", side_effect=PermissionError("denied")), patch(
|
with patch("pathlib.Path.read_bytes", side_effect=PermissionError("denied")), patch(
|
||||||
"httpx.AsyncClient.post", post
|
"httpx.AsyncClient.post", post
|
||||||
):
|
):
|
||||||
result = await provider.transcribe(audio_file)
|
result = await provider.transcribe(audio_file)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user