Fix test isolation: save/restore pre_call_rules and post_call_rules

test_post_call_rule_streaming in test_rules.py sets
litellm.post_call_rules but never cleans up. Since
pytest_collection_modifyitems sorts tests by name across modules,
the leaked rule causes failures in test_streaming.py,
test_register_model.py, and test_sagemaker.py.

Add pre_call_rules and post_call_rules to the isolate_litellm_state
fixture's save/restore and clear lists.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yuneng-jiang
2026-03-15 13:28:14 -07:00
co-authored by Claude Opus 4.6
parent 568726b06e
commit 968d7a3eca
+9 -1
View File
@@ -43,6 +43,12 @@ def isolate_litellm_state():
if hasattr(litellm, attr):
original_state[attr] = getattr(litellm, attr)
# Save rules that tests may set (e.g. test_rules.py)
for attr in ("pre_call_rules", "post_call_rules"):
if hasattr(litellm, attr):
val = getattr(litellm, attr)
original_state[attr] = val.copy() if val else []
# Save transport/network globals
for attr in ("disable_aiohttp_transport", "force_ipv4"):
if hasattr(litellm, attr):
@@ -52,13 +58,15 @@ def isolate_litellm_state():
if hasattr(litellm, "in_memory_llm_clients_cache"):
litellm.in_memory_llm_clients_cache.flush_cache()
# Clear callbacks before test
# Clear callbacks and rules before test
for attr in (
"callbacks",
"success_callback",
"failure_callback",
"_async_success_callback",
"_async_failure_callback",
"pre_call_rules",
"post_call_rules",
):
if hasattr(litellm, attr):
setattr(litellm, attr, [])