Skip to content

test(interactions): drop the save/restore scaffolding around the legacy flag - #37841

Merged
yuneng-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_gemini_interactions_test_globals
Aug 22, 2026
Merged

test(interactions): drop the save/restore scaffolding around the legacy flag#37841
yuneng-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_gemini_interactions_test_globals

Conversation

@yuneng-berri

@yuneng-berri yuneng-berri commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Seven tests capture the flag, wrap the body in try, restore in finally
  • The scaffolding costs a level of indentation in every body
  • Fourteen of the twenty TQ005 in the file come from it

How it solves it:

  • monkeypatch.setattr replaces the capture and the finally
  • The bodies flatten by one level
  • 20 TQ005 drop to 6, and the 6 that stay are deliberate

User Flow

This one is test-only scaffolding with no user-visible behavior, so there is no before and after flow to walk. The tests assert the same things on the same code, and the same 36 pass either way

Relevant issues

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

To be plain about what this does and does not prove: the try/finally it removes already restored the global, so the run reads "restored" on both sides. That is the point. The bookkeeping was correct and hand-written around every body, and monkeypatch does the same job where the value is set. What changes is the count the quality rules see.

Shared setup: run the file, read the global back in the same interpreter, then count what the quality rules see in it.

uv run python -c '
import litellm, pytest
A = "use_legacy_interactions_schema"
before = getattr(litellm, A, None)
pytest.main(["tests/test_litellm/interactions/test_gemini_interactions_transformation.py", "-q", "-p", "no:randomly"])
print("litellm." + A + " after the run:", "restored" if getattr(litellm, A, None) == before else "LEAKED")
'
uv run python scripts/check_test_quality.py tests/test_litellm/interactions/test_gemini_interactions_transformation.py | grep -oE "TQ00[0-9]" | sort | uniq -c

Before (ff02d5c)

  1. The first command
36 passed, 1 warning in 0.24s
litellm.use_legacy_interactions_schema after the run: restored
  1. The second command
   1 TQ003
  20 TQ005

After (83743ab)

  1. The first command
36 passed, 1 warning in 0.20s
litellm.use_legacy_interactions_schema after the run: restored
  1. The second command
   1 TQ003
   6 TQ005
  1. uv run python scripts/test_quality_gate.py --base origin/litellm_internal_staging
OK: every TQ rule is within its test-suite ceiling (base origin/litellm_internal_staging)

Type

🧹 Refactoring
✅ Test

Caveats (if any)

  • Six restores stay: they scope the flag to one constructor call
  • Widening them to a whole test would change what streaming asserts

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

…cy flag

Seven tests captured litellm.use_legacy_interactions_schema, wrapped their body
in a try, and put it back in a finally. monkeypatch.setattr does that, so the
capture, the try and the finally go and the bodies lose an indentation level.

The remaining hand-rolled restores stay. They hold the flag only across the
iterator's constructor and put it back before the test iterates, so handing
them to monkeypatch would widen that window to the whole test and change what
the streaming assertions run against.
@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This test-only refactor replaces manual save/restore blocks for the legacy interactions-schema flag with pytest’s function-scoped monkeypatch fixture while preserving the existing assertions and cleanup behavior.

  • Flattens seven tests by removing try/finally restoration scaffolding.
  • Lowers the TQ005 quality budget by fourteen to match the removed global assignments.

Confidence Score: 5/5

The PR appears safe to merge because the fixture teardown preserves test isolation and the quality-budget adjustment matches the removed violations.

The changed tests retain the same flag values and assertions while pytest restores the previous module attribute after each test, and the fourteen-point TQ005 reduction corresponds exactly to the removed qualifying assignments.

Important Files Changed

Filename Overview
tests/test_litellm/interactions/test_gemini_interactions_transformation.py Replaces seven manual global-flag restoration blocks with equivalent pytest monkeypatch lifecycle management without changing tested behavior.
test-quality-budget.json Ratchets the TQ005 ceiling down by fourteen, exactly matching the fourteen assignments removed from the test file.

Reviews (1): Last reviewed commit: "test(interactions): drop the save/restor..." | Re-trigger Greptile

@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…itellm_gemini_interactions_test_globals

# Conflicts:
#	test-quality-budget.json
@yuneng-berri
yuneng-berri merged commit 322293a into litellm_internal_staging Aug 22, 2026
68 checks passed
@yuneng-berri
yuneng-berri deleted the litellm_gemini_interactions_test_globals branch August 22, 2026 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants