mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-21 00:21:49 +00:00
refactor(rust): parse env boolean with pydantic
This commit is contained in:
parent
ed8b6968fc
commit
72e511f9c7
2 changed files with 31 additions and 2 deletions
|
|
@ -6,9 +6,11 @@ from enum import Enum
|
|||
from types import MappingProxyType
|
||||
from typing import Final
|
||||
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
DEFAULT_RUST_ENABLED: Final = False
|
||||
_TRUE_ENV_VALUES: Final = frozenset({"1", "true", "yes", "on"})
|
||||
_GLOBAL_ENV_NAME: Final = "LITELLM_RUST"
|
||||
_ENV_BOOL: Final = TypeAdapter(bool)
|
||||
|
||||
|
||||
class RouteName(str, Enum):
|
||||
|
|
@ -65,7 +67,7 @@ _CONFIGURATION: Final = _RustConfiguration()
|
|||
def _parse_env_bool(value: str | None) -> bool | None:
|
||||
if value is None:
|
||||
return None
|
||||
return value.strip().lower() in _TRUE_ENV_VALUES
|
||||
return _ENV_BOOL.validate_python(value.strip())
|
||||
|
||||
|
||||
def resolve_rust_enabled(
|
||||
|
|
|
|||
27
tests/test_litellm/rust_bridge/test_configuration_env.py
Normal file
27
tests/test_litellm/rust_bridge/test_configuration_env.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from litellm.rust_bridge.configuration import (
|
||||
_parse_env_bool, # pyright: ignore[reportPrivateUsage] # directly test env parsing contract
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ("1", "true", "t", "yes", "y", "on", "TRUE", " yes "))
|
||||
def test_parse_env_bool_accepts_standard_true_values(value: str) -> None:
|
||||
assert _parse_env_bool(value) is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ("0", "false", "f", "no", "n", "off", "FALSE", " no "))
|
||||
def test_parse_env_bool_accepts_standard_false_values(value: str) -> None:
|
||||
assert _parse_env_bool(value) is False
|
||||
|
||||
|
||||
def test_parse_env_bool_preserves_unset_value() -> None:
|
||||
assert _parse_env_bool(None) is None
|
||||
|
||||
|
||||
def test_parse_env_bool_rejects_unknown_value() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
_parse_env_bool("enabled")
|
||||
Loading…
Add table
Reference in a new issue