diff --git a/src/deepgram/client.py b/src/deepgram/client.py index 014fc2f2..56e98d11 100644 --- a/src/deepgram/client.py +++ b/src/deepgram/client.py @@ -115,7 +115,7 @@ def __init__(self, *args, **kwargs) -> None: # Set a placeholder api_key if none provided (base client requires it) if kwargs.get("api_key") is None: kwargs["api_key"] = "token" - elif kwargs.get("api_key") is None: + elif "api_key" not in kwargs: # The generated base client takes os.getenv("DEEPGRAM_API_KEY") as a default # argument, so it is read once at import. Re-read it here so a key set after # import (load_dotenv below the imports) is still picked up. @@ -199,7 +199,7 @@ def __init__(self, *args, **kwargs) -> None: # Set a placeholder api_key if none provided (base client requires it) if kwargs.get("api_key") is None: kwargs["api_key"] = "token" - elif kwargs.get("api_key") is None: + elif "api_key" not in kwargs: # The generated base client takes os.getenv("DEEPGRAM_API_KEY") as a default # argument, so it is read once at import. Re-read it here so a key set after # import (load_dotenv below the imports) is still picked up. diff --git a/tests/custom/test_api_key_env_resolution.py b/tests/custom/test_api_key_env_resolution.py index abe61e7e..8e7dacc7 100644 --- a/tests/custom/test_api_key_env_resolution.py +++ b/tests/custom/test_api_key_env_resolution.py @@ -8,6 +8,10 @@ working (issue #734). """ +import os +import subprocess +import sys +import textwrap import typing import pytest @@ -25,13 +29,31 @@ def _auth(client: typing.Any) -> typing.Optional[str]: return client._client_wrapper.api_key -def test_env_key_set_after_import_is_used( - no_env_key: None, monkeypatch: pytest.MonkeyPatch -) -> None: - """The import already happened above with no key set; this is the load_dotenv case.""" - monkeypatch.setenv("DEEPGRAM_API_KEY", "set-after-import") - assert _auth(DeepgramClient()) == "set-after-import" - assert _auth(AsyncDeepgramClient()) == "set-after-import" +def test_env_key_set_after_import_is_used() -> None: + """Exercise the load_dotenv ordering in a process that starts without a key.""" + env = os.environ.copy() + env.pop("DEEPGRAM_API_KEY", None) + code = textwrap.dedent( + """ + import os + + from deepgram import AsyncDeepgramClient, DeepgramClient + + os.environ["DEEPGRAM_API_KEY"] = "set-after-import" + assert DeepgramClient()._client_wrapper.api_key == "set-after-import" + assert AsyncDeepgramClient()._client_wrapper.api_key == "set-after-import" + """ + ) + + result = subprocess.run( + [sys.executable, "-c", code], + env=env, + capture_output=True, + text=True, + check=False, + ) + + assert result.returncode == 0, result.stderr def test_explicit_api_key_beats_the_environment(monkeypatch: pytest.MonkeyPatch) -> None: @@ -40,6 +62,14 @@ def test_explicit_api_key_beats_the_environment(monkeypatch: pytest.MonkeyPatch) assert _auth(AsyncDeepgramClient(api_key="explicit")) == "explicit" +def test_explicit_none_does_not_fall_back_to_environment(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("DEEPGRAM_API_KEY", "from-env") + with pytest.raises(ApiError): + DeepgramClient(api_key=None) + with pytest.raises(ApiError): + AsyncDeepgramClient(api_key=None) + + def test_access_token_still_takes_precedence(monkeypatch: pytest.MonkeyPatch) -> None: """access_token callers get the placeholder, not the environment key.""" monkeypatch.setenv("DEEPGRAM_API_KEY", "from-env")