test(key-management): unwind the global writes the key tests scaffold around - #37822
Merged
yuneng-berri merged 2 commits intoAug 22, 2026
Merged
Conversation
… around Seventeen tests in this file save a litellm module global, open a try, write it, and restore it in a finally. Four more sit behind autouse fixtures that reset the flag to a hard-coded False rather than to whatever it was. monkeypatch.setattr does all of that, so the capture, the try and the finally come out and the test body loses a level of indentation. The alias-format fixtures stop guessing the value they are restoring to. Also drops the sys.path.insert, whose argument resolves four levels above the repo, so it was never what made the imports work. TQ003 1077 -> 1076 and TQ005 2836 -> 2796, and the budget ceilings come down with them. 443 tests pass either way; the conftest snapshot was already catching these globals, so this is about not needing it.
Contributor
Greptile SummaryThis test-only refactor replaces manual writes and restoration of key-management globals with pytest monkeypatching and removes an ineffective path mutation
Confidence Score: 5/5The PR appears safe to merge because it only improves test isolation and ratchets matching quality budgets The changed tests preserve their configured values and assertions while pytest now restores global state even when a test exits exceptionally
|
| Filename | Overview |
|---|---|
| tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py | Replaces manual global mutation scaffolding with function-scoped monkeypatch restoration without weakening the existing assertions |
| test-quality-budget.json | Ratchets TQ003 and TQ005 ceilings to the reported post-refactor counts |
Reviews (1): Last reviewed commit: "test(key-management): unwind the global ..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
ryan-crabbe-berri
approved these changes
Aug 22, 2026
yucheng-berri
approved these changes
Aug 22, 2026
…itellm_key_management_global_writes # Conflicts: # test-quality-budget.json
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.
TLDR
Problem this solves:
How it solves it:
User Flow
No end-user behavior changes. A proxy admin calling
https://litellm-domain/key/generate gets the same limits enforced the same way;
this is the test file around that endpoint, not the endpoint.
Relevant issues
Linear ticket
Pre-Submission checklist
Screenshots / Proof of Fix
Shared setup: run the file, then read the three globals back in the same
interpreter, and count what the quality rules see.
Before (ff02d5c)
uv run python scripts/check_test_quality.py tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py | grep -o 'TQ00[0-9]' | sort | uniq -cAfter (1f682f8)
uv run python scripts/test_quality_gate.pyType
🧹 Refactoring
✅ Test
Caveats (if any)
test-quality-budget.json, as do test: drop the cwd-relative sys.path.insert calls from the test suite #37802, test: unwind environment writes in tests/test_litellm with monkeypatch #37806 and test(cost-calc): stop 182 global writes leaking out of the cost-calc suites #37815Final Attestation