mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-04 23:07:50 +00:00
74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
import ast
|
|
import inspect
|
|
import json
|
|
import os
|
|
import sys
|
|
from unittest import mock
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
from fastapi.testclient import TestClient
|
|
|
|
sys.path.insert(0, os.path.abspath("../..")) #
|
|
|
|
import importlib
|
|
|
|
import litellm
|
|
from litellm import constants
|
|
|
|
|
|
def _build_constant_env_var_map() -> dict[str, str]:
|
|
"""
|
|
Build a mapping of CONSTANT_NAME -> ENV_VAR_NAME by parsing constants.py.
|
|
|
|
This keeps the test resilient when a constant name and env var name differ
|
|
(e.g., aliases like LITELLM_* env vars).
|
|
"""
|
|
env_var_map: dict[str, str] = {}
|
|
constants_source = inspect.getsource(constants)
|
|
parsed = ast.parse(constants_source)
|
|
|
|
for node in parsed.body:
|
|
if not isinstance(node, ast.Assign):
|
|
continue
|
|
|
|
if len(node.targets) != 1 or not isinstance(node.targets[0], ast.Name):
|
|
continue
|
|
|
|
constant_name = node.targets[0].id
|
|
env_var_name = None
|
|
|
|
for child in ast.walk(node.value):
|
|
if not isinstance(child, ast.Call):
|
|
continue
|
|
|
|
# os.getenv("ENV_NAME", default)
|
|
if (
|
|
isinstance(child.func, ast.Attribute)
|
|
and isinstance(child.func.value, ast.Name)
|
|
and child.func.value.id == "os"
|
|
and child.func.attr == "getenv"
|
|
and len(child.args) >= 1
|
|
and isinstance(child.args[0], ast.Constant)
|
|
and isinstance(child.args[0].value, str)
|
|
):
|
|
env_var_name = child.args[0].value
|
|
break
|
|
|
|
# get_env_int("ENV_NAME", default)
|
|
if (
|
|
isinstance(child.func, ast.Name)
|
|
and child.func.id == "get_env_int"
|
|
and len(child.args) >= 1
|
|
and isinstance(child.args[0], ast.Constant)
|
|
and isinstance(child.args[0].value, str)
|
|
):
|
|
env_var_name = child.args[0].value
|
|
break
|
|
|
|
if env_var_name:
|
|
env_var_map[constant_name] = env_var_name
|
|
|
|
return env_var_map
|