Skip to content

Commit 434add7

Browse files
Merge pull request #38274 from BerriAI/litellm_test_lint_os_environ
test: gate the test tree on B003 so a test cannot swap os.environ for a plain dict
2 parents 50a42ba + 8298cbe commit 434add7

2 files changed

Lines changed: 17 additions & 23 deletions

File tree

ruff-tests.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
# F632 `is` against a literal. It compares identity, so it passes only where CPython
5252
# happens to intern the value and stops meaning what it says the moment the
5353
# value is built at runtime
54+
# B003 `os.environ = {...}` rebinds the mapping instead of mutating it, so `putenv`
55+
# never fires and a subprocess still reads the real keys the test believes it
56+
# cleared. The manual restore underneath is skipped whenever the body raises,
57+
# so every later test in that worker inherits a plain dict for an environment
5458
#
5559
# No target-version here on purpose: it resolves from requires-python (>=3.10), so
5660
# 3.11-only builtins like BaseExceptionGroup are correctly flagged in a tree that
@@ -78,4 +82,5 @@ lint.select = [
7882
"B023",
7983
"B025",
8084
"F632",
85+
"B003",
8186
]

tests/litellm_utils_tests/test_utils.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -328,34 +328,23 @@ def test_trimming_with_untokenizable_field(caplog: pytest.LogCaptureFixture) ->
328328

329329

330330
def test_aget_valid_models():
331-
old_environ = os.environ
332-
os.environ = {"OPENAI_API_KEY": "temp"} # mock set only openai key in environ
331+
with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "temp"}, clear=True):
332+
valid_models = get_valid_models()
333+
print(valid_models)
333334

334-
valid_models = get_valid_models()
335-
print(valid_models)
336-
337-
# list of openai supported llms on litellm
338-
expected_models = (
339-
litellm.open_ai_chat_completion_models | litellm.open_ai_text_completion_models
340-
)
341-
342-
assert set(valid_models) == set(expected_models)
335+
# list of openai supported llms on litellm
336+
expected_models = (
337+
litellm.open_ai_chat_completion_models | litellm.open_ai_text_completion_models
338+
)
343339

344-
# reset replicate env key
345-
os.environ = old_environ
340+
assert set(valid_models) == set(expected_models)
346341

347342
# GEMINI
348-
expected_models = litellm.gemini_models
349-
old_environ = os.environ
350-
os.environ = {"GEMINI_API_KEY": "temp"} # mock set only openai key in environ
351-
352-
valid_models = get_valid_models()
353-
354-
print(valid_models)
355-
assert set(valid_models) == set(expected_models)
343+
with mock.patch.dict(os.environ, {"GEMINI_API_KEY": "temp"}, clear=True):
344+
valid_models = get_valid_models()
356345

357-
# reset replicate env key
358-
os.environ = old_environ
346+
print(valid_models)
347+
assert set(valid_models) == set(litellm.gemini_models)
359348

360349

361350
@pytest.mark.parametrize("custom_llm_provider", ["anthropic", "xai"])

0 commit comments

Comments
 (0)