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.
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.