litellm/tests/unit/test_lazy_imports.py
yuneng-jiang da55893656
ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864)
* test(unit): reset module state only when a test changed it

The proxy conftest deep-copied every snapshotted litellm attribute before each
test, and the copy of litellm.model_cost alone was a median 0.12 s for each of
56,031 proxy tests on the runner. The reset now copies an attribute back only
when it no longer equals the snapshot.

The enterprise and MCP server conftests reloaded litellm before every test,
3.27 s and 0.21 s per test. They now use the same reset.

* test(unit): remove six flaky tests

They failed on main without a code change behind them between 2026-11-05 and
2026-11-14: the vector store hook SDK fallback, the concurrent user config TPM
count, three Prometheus metrics server port tests and the OpenCode list cost
billing test.

* ci: run four heavy unit shards on 16-core runners and keep two smoke jobs off pull requests

proxy-feature-endpoints, proxy-endpoints, root and llms-providers ran 1.9 to 2.2
times faster with 8 workers on a 16-core runner in a benchmark run.

windows-sdk and proxy-build-from-pip still run on pushes to main and manual
runs. proxy-build-from-pip installs a published release and never runs the
pull request's code.

* test: move 109 tests that start a process or open a socket into tests/integration

tests/unit is in-process by its own rules, and these tests spawn a Python
interpreter, a shell or a local TCP or HTTP server. They took 921 s of unit
shard time per run, 396 s of it in the MCP client transport tests. Each one now
lives in a *_real_io.py file under the integration directory for its domain and
runs with that directory's group.

The tests themselves are unchanged apart from one repository-root path in the
CLI test.

* test(unit): copy back only the dict entries a test changed

A test module that registers models at import leaves litellm.model_cost four
entries larger than the snapshot before every test in its directory, so the
reset still deep-copied the whole price map for each of them. It now rebuilds a
changed dict from the live entries that still match and copies only the rest.

* ci(smoke): keep the two jobs that skip on pull requests out of the smoke gate

A skipped job passing the gate is what test_unit_passed_gate guards against,
so the gate goes back to requiring success from every job it needs and
windows-sdk and proxy-build-from-pip stop being part of it. They still run
and report on pushes to main and manual runs.
2026-11-14 15:41:30 -08:00

91 lines
3.7 KiB
Python

import importlib
from typing import Final
import pytest
import litellm
import litellm.utils
from litellm._lazy_imports_registry import LITELLM_ATTRIBUTES, UTILS_ATTRIBUTES
from tests.test_litellm_rust.support.child_interpreter import run_child_interpreter
def _declared(location: tuple[str, str]) -> object:
module_path, attr_name = location
return getattr(importlib.import_module(module_path, package="litellm"), attr_name)
@pytest.mark.parametrize("name", sorted(LITELLM_ATTRIBUTES))
def test_litellm_attribute_is_the_declared_object(name: str):
assert getattr(litellm, name) is _declared(LITELLM_ATTRIBUTES[name])
@pytest.mark.parametrize("name", sorted(UTILS_ATTRIBUTES))
def test_utils_attribute_is_the_same_object_from_litellm_and_litellm_utils(name: str):
expected: Final = _declared(UTILS_ATTRIBUTES[name])
assert getattr(litellm.utils, name) is expected
assert getattr(litellm, name) is expected
@pytest.mark.parametrize(
("instance_name", "class_name"),
[
("in_memory_llm_clients_cache", "LLMClientCache"),
("openaiOSeriesConfig", "OpenAIOSeriesConfig"),
("openAIGPTConfig", "OpenAIGPTConfig"),
("openAIGPTAudioConfig", "OpenAIGPTAudioConfig"),
("openAIGPT5Config", "OpenAIGPT5Config"),
("nvidiaNimConfig", "NvidiaNimConfig"),
("nvidiaNimEmbeddingConfig", "NvidiaNimEmbeddingConfig"),
("priority_reservation_settings", "PriorityReservationSettings"),
("logging_callback_manager", "LoggingCallbackManager"),
],
)
def test_lazy_instance_is_a_single_instance_of_its_class(instance_name: str, class_name: str):
instance: Final = getattr(litellm, instance_name)
assert type(instance) is getattr(litellm, class_name)
assert getattr(litellm, instance_name) is instance
def test_module_level_http_clients_use_request_timeout(monkeypatch: pytest.MonkeyPatch):
client_names: Final = ("module_level_client", "module_level_aclient")
monkeypatch.setattr(litellm, "request_timeout", 7)
for client_name in client_names:
monkeypatch.delitem(vars(litellm), client_name, raising=False)
try:
assert litellm.module_level_client.timeout == 7
assert litellm.module_level_aclient.timeout == 7
assert litellm.module_level_client is litellm.module_level_client
finally:
for client_name in client_names:
vars(litellm).pop(client_name, None)
def test_provider_list_lists_every_provider():
assert litellm.provider_list == list(litellm.LlmProviders)
@pytest.mark.parametrize("name", ["proxy", "guardrails", "harness", "_service_logger"])
def test_submodule_attribute_is_the_submodule(name: str):
assert getattr(litellm, name) is importlib.import_module(f"litellm.{name}")
@pytest.mark.parametrize("module", [litellm, litellm.utils])
def test_unknown_attribute_raises_attribute_error(module: object):
with pytest.raises(AttributeError, match="not_a_litellm_attribute"):
getattr(module, "not_a_litellm_attribute")
assert not hasattr(module, "not_a_litellm_attribute")
def test_litellm_instances_are_not_attributes_of_litellm_utils():
assert not hasattr(litellm.utils, "module_level_client")
@pytest.mark.parametrize(
"module",
["litellm.litellm_core_utils.get_litellm_params", "litellm.batches.batch_utils", "litellm.types.utils"],
)
def test_kwargs_funnel_and_its_importers_load_first_in_fresh_process(module: str):
"""These modules are often the first to pull in litellm.types.utils, and the WIF key sets shared between
the funnel and all_litellm_params must not turn that into a cycle."""
result = run_child_interpreter(f"import {module}", timeout=120)
assert result.returncode == 0, result.stderr