bug: fix unreachable None guard in apikey_from_env#434
Open
NishchayMahor wants to merge 1 commit into
Open
Conversation
apikey_from_env computed env_key = provider.upper()... before the 'if provider is None: return ""' guard, so apikey_from_env(None) raised AttributeError instead of returning "" (the guard was dead code). This is reachable via LLMOptions.parse_api_key when provider is None. Move the None check above the dereference, and add offline unit tests.
There was a problem hiding this comment.
Pull request overview
Fixes a crash in apikey_from_env when provider=None by moving the None guard before dereferencing provider, and adds unit tests to cover the regression and env var normalization behavior.
Changes:
- Reorders the
provider is Noneguard inapikey_from_envto prevent anAttributeErrorforNoneproviders. - Adds
tests/test_utils.pywith coverage forNone, env lookup, and dash-to-underscore normalization.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
portkey_ai/api_resources/utils.py |
Moves the None check ahead of the provider.upper() dereference to avoid a runtime crash. |
tests/test_utils.py |
Adds regression/unit tests for apikey_from_env behavior (None handling + env key normalization). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
387
to
+390
| def apikey_from_env(provider: Union[ProviderTypes, ProviderTypesLiteral, str]) -> str: | ||
| env_key = f"{provider.upper().replace('-', '_')}_API_KEY" | ||
| if provider is None: | ||
| return "" | ||
| env_key = f"{provider.upper().replace('-', '_')}_API_KEY" |
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.
What
apikey_from_envhas aif provider is None: return ""guard, but it sits after the line that dereferencesprovider, so it's unreachable — aNoneprovider crashes instead:This is reachable through
LLMOptions.parse_api_key, which callsapikey_from_env(values.get("provider", "")); constructingLLMOptionswithprovider=None(and noapi_key/virtual_key) crashes with an opaqueAttributeErrorinstead of the intended empty-string behavior.Fix
Move the
Nonecheck above the dereference, so the guard actually runs:Testing
Added
tests/test_utils.py(offline, no network/API key): assertsapikey_from_env(None) == "", plus positive cases (env lookup and dash normalization viamonkeypatch.setenv).pytest tests/test_utils.py→ 3 passed; theNonecase raisesAttributeErroronmainwithout this change.black/ruffclean.