Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
Fix auth token dotenv precedence
Alishahryar19c28af7cf1ff
diff --git a/config/settings.py b/config/settings.pyindex 5544c88a..26111055 100644--- a/config/settings.py+++ b/config/settings.py@@ -6,6 +6,7 @@ from functools import lru_cachefrom pathlib import Pathfrom typing import Any+from dotenv import dotenv_valuesfrom pydantic import Field, field_validator, model_validatorfrom pydantic_settings import BaseSettings, SettingsConfigDict@@ -35,23 +36,33 @@ def _configured_env_files(model_config: Mapping[str, Any]) -> tuple[Path, ...]:def _env_file_contains_key(path: Path, key: str) -> bool:"""Check whether a dotenv-style file defines the given key."""+ return _env_file_value(path, key) is not None+++def _env_file_value(path: Path, key: str) -> str | None:+ """Return a dotenv value when the file explicitly defines the key."""if not path.is_file():- return False+ return Nonetry:- for line in path.read_text(encoding="utf-8").splitlines():- stripped = line.strip()- if not stripped or stripped.startswith("#"):- continue- if stripped.startswith("export "):- stripped = stripped[7:].lstrip()- name, sep, _value = stripped.partition("=")- if sep and name.strip() == key:- return True+ values = dotenv_values(path)except OSError:- return False+ return None++ if key not in values:+ return None+ value = values[key]+ return "" if value is None else value+- return False+def _env_file_override(model_config: Mapping[str, Any], key: str) -> str | None:+ """Return the last configured dotenv value that explicitly defines a key."""+ configured_value: str | None = None+ for env_file in _configured_env_files(model_config):+ value = _env_file_value(env_file, key)+ if value is not None:+ configured_value = value+ return configured_valuedef _removed_env_var_message(model_config: Mapping[str, Any]) -> str | None:@@ -281,6 +292,14 @@ class Settings(BaseSettings):)return self+ @model_validator(mode="after")+ def prefer_dotenv_anthropic_auth_token(self) -> Settings:+ """Let explicit .env auth config override stale shell/client tokens."""+ dotenv_value = _env_file_override(self.model_config, "ANTHROPIC_AUTH_TOKEN")+ if dotenv_value is not None:+ self.anthropic_auth_token = dotenv_value+ return self+@propertydef provider_type(self) -> str:"""Extract provider type from the default model string."""diff --git a/tests/config/test_config.py b/tests/config/test_config.pyindex 8e9ac8c0..a543458b 100644--- a/tests/config/test_config.py+++ b/tests/config/test_config.py@@ -127,6 +127,46 @@ class TestSettings:assert settings.haiku_enable_thinking is Falseassert settings.model_enable_thinking is True+ def test_anthropic_auth_token_from_env_without_dotenv_key(self, monkeypatch):+ """ANTHROPIC_AUTH_TOKEN env var is loaded when dotenv does not define it."""+ from config.settings import Settings++ monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "process-token")+ monkeypatch.setitem(Settings.model_config, "env_file", ())+ settings = Settings()+ assert settings.anthropic_auth_token == "process-token"++ def test_empty_dotenv_anthropic_auth_token_overrides_process_env(+ self, monkeypatch, tmp_path+ ):+ """An explicit empty .env token disables auth despite stale shell tokens."""+ from config.settings import Settings++ env_file = tmp_path / ".env"+ env_file.write_text("ANTHROPIC_AUTH_TOKEN=\n", encoding="utf-8")+ monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "stale-client-token")+ monkeypatch.setitem(Settings.model_config, "env_file", (env_file,))++ settings = Settings()+ assert settings.anthropic_auth_token == ""++ def test_dotenv_anthropic_auth_token_overrides_process_env(+ self, monkeypatch, tmp_path+ ):+ """A configured .env token is the server token even with a stale shell token."""+ from config.settings import Settings++ env_file = tmp_path / ".env"+ env_file.write_text(+ 'ANTHROPIC_AUTH_TOKEN="server-token"\n',+ encoding="utf-8",+ )+ monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "stale-client-token")+ monkeypatch.setitem(Settings.model_config, "env_file", (env_file,))++ settings = Settings()+ assert settings.anthropic_auth_token == "server-token"+def test_removed_nim_enable_thinking_raises(self, monkeypatch):"""NIM_ENABLE_THINKING now fails fast with a migration message."""from config.settings import Settings