Skip to content

Commit 17bfd41

Browse files
authored
chore: migrate Python formatter from black to ruff format (BerriAI#31317)
1 parent 6db55e0 commit 17bfd41

215 files changed

Lines changed: 1877 additions & 1241 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test-linting.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ jobs:
5050
run: |
5151
uv sync --frozen
5252
53-
- name: Check Black formatting
53+
- name: Check ruff format
5454
run: |
5555
cd litellm
56-
uv run --no-sync black --check --exclude '/enterprise/' .
56+
uv run --no-sync ruff format --check --line-length 88 --exclude '/enterprise/' .
5757
cd ..
5858
5959
- name: Debug - Check file state

Makefile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ help:
2020
@echo " make install-test-deps - Install the full local test environment"
2121
@echo " make install-helm-unittest - Install helm unittest plugin"
2222
@echo " make install-hooks - Install git hooks (Conventional Commits + Branches)"
23-
@echo " make format - Apply Black code formatting"
24-
@echo " make format-check - Check Black code formatting (matches CI)"
25-
@echo " make lint - Run all linting (Ruff, basedpyright, Black check, circular imports, import safety)"
23+
@echo " make format - Apply ruff format code formatting"
24+
@echo " make format-check - Check ruff format code formatting (matches CI)"
25+
@echo " make lint - Run all linting (Ruff, basedpyright, format check, circular imports, import safety)"
2626
@echo " make lint-ruff - Run Ruff linting only"
2727
@echo " make lint-basedpyright - Run basedpyright strict, gated by per-rule error counts"
2828
@echo " make lint-basedpyright-budget-update - Re-capture the basedpyright per-rule budget (ratchet)"
29-
@echo " make lint-black - Check Black formatting (matches CI)"
29+
@echo " make lint-format - Check ruff format formatting (matches CI)"
3030
@echo " make lint-ruff-budget - Gate the codebase total of each strict ruff rule against its ceiling"
3131
@echo " make lint-gate - Strict ruff gate in CI-parity mode (fetches staging, simulates the merge)"
3232
@echo " make lint-ruff-budget-update - Re-capture per-rule baselines in ruff-strict-budget.json (ratchet)"
@@ -82,11 +82,13 @@ install-hooks:
8282
./scripts/install_git_hooks.sh
8383

8484
# Formatting
85+
# 88-column wrap matches the Black width the whole repo is formatted to; ruff.toml's
86+
# global line-length is 120 (for E501/isort), so 88 is forced here.
8587
format: install-dev
86-
cd litellm && $(UV_RUN) black . && cd ..
88+
cd litellm && $(UV_RUN) ruff format --line-length 88 --exclude '/enterprise/' . && cd ..
8789

8890
format-check: install-dev
89-
cd litellm && $(UV_RUN) black --check . && cd ..
91+
cd litellm && $(UV_RUN) ruff format --check --line-length 88 --exclude '/enterprise/' . && cd ..
9092

9193
# Linting targets
9294
lint-ruff: install-dev
@@ -131,7 +133,7 @@ lint-basedpyright: install-dev
131133
lint-basedpyright-budget-update: install-dev
132134
($(UV_RUN) basedpyright --outputjson || true) | $(UV_RUN) python scripts/type_check_gate.py --update
133135

134-
lint-black: format-check
136+
lint-format: format-check
135137

136138
lint-ruff-budget: install-dev
137139
$(UV_RUN) python scripts/ruff_strict_gate.py

litellm/__init__.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,8 @@ def _dev_env_hot_reload_enabled() -> bool:
390390
enable_caching_on_provider_specific_optional_params: bool = (
391391
False # feature-flag for caching on optional params - e.g. 'top_k'
392392
)
393-
caching: bool = (
394-
False # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648
395-
)
396-
caching_with_models: bool = (
397-
False # # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648
398-
)
393+
caching: bool = False # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648
394+
caching_with_models: bool = False # # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648
399395
cache: Optional["Cache"] = (
400396
None # cache object <- use this - https://docs.litellm.ai/docs/caching
401397
)
@@ -416,9 +412,7 @@ def _dev_env_hot_reload_enabled() -> bool:
416412

417413
_current_cost = 0.0 # private variable, used if max budget is set
418414
error_logs: Dict = {}
419-
add_function_to_prompt: bool = (
420-
False # if function calling not supported by api, append function call details to system prompt
421-
)
415+
add_function_to_prompt: bool = False # if function calling not supported by api, append function call details to system prompt
422416
client_session: Optional[httpx.Client] = None
423417
aclient_session: Optional[httpx.AsyncClient] = None
424418
model_fallbacks: Optional[List] = None # Deprecated for 'litellm.fallbacks'
@@ -485,9 +479,7 @@ def _dev_env_hot_reload_enabled() -> bool:
485479
disable_add_prefix_to_prompt: bool = (
486480
False # used by anthropic, to disable adding prefix to prompt
487481
)
488-
disable_copilot_system_to_assistant: bool = (
489-
False # If false (default), converts all 'system' role messages to 'assistant' for GitHub Copilot compatibility. Set to true to disable this behavior.
490-
)
482+
disable_copilot_system_to_assistant: bool = False # If false (default), converts all 'system' role messages to 'assistant' for GitHub Copilot compatibility. Set to true to disable this behavior.
491483
public_mcp_servers: Optional[List[str]] = None
492484
public_mcp_hub_strict_whitelist: bool = True
493485
public_model_groups: Optional[List[str]] = None
@@ -507,17 +499,13 @@ def _dev_env_hot_reload_enabled() -> bool:
507499

508500

509501
######## Networking Settings ########
510-
use_aiohttp_transport: bool = (
511-
True # Older variable, aiohttp is now the default. use disable_aiohttp_transport instead.
512-
)
502+
use_aiohttp_transport: bool = True # Older variable, aiohttp is now the default. use disable_aiohttp_transport instead.
513503
aiohttp_trust_env: bool = False # set to true to use HTTP_ Proxy settings
514504
disable_aiohttp_transport: bool = False # Set this to true to use httpx instead
515505
disable_aiohttp_trust_env: bool = (
516506
False # When False, aiohttp will respect HTTP(S)_PROXY env vars
517507
)
518-
force_ipv4: bool = (
519-
False # when True, litellm will force ipv4 for all LLM requests. Some users have seen httpx ConnectionError when using ipv6.
520-
)
508+
force_ipv4: bool = False # when True, litellm will force ipv4 for all LLM requests. Some users have seen httpx ConnectionError when using ipv6.
521509
network_mock: bool = False # When True, use mock transport — no real network calls
522510

523511
####### STOP SEQUENCE LIMIT #######
@@ -551,12 +539,12 @@ def _dev_env_hot_reload_enabled() -> bool:
551539
from litellm.litellm_core_utils.get_model_cost_map import get_model_cost_map
552540

553541
model_cost = get_model_cost_map(url=model_cost_map_url)
554-
cost_discount_config: Dict[str, float] = (
555-
{}
556-
) # Provider-specific cost discounts {"vertex_ai": 0.05} = 5% discount
557-
cost_margin_config: Dict[str, Union[float, Dict[str, float]]] = (
558-
{}
559-
) # Provider-specific or global cost margins. Examples:
542+
cost_discount_config: Dict[
543+
str, float
544+
] = {} # Provider-specific cost discounts {"vertex_ai": 0.05} = 5% discount
545+
cost_margin_config: Dict[
546+
str, Union[float, Dict[str, float]]
547+
] = {} # Provider-specific or global cost margins. Examples:
560548
# Percentage: {"openai": 0.10} = 10% margin
561549
# Fixed: {"openai": {"fixed_amount": 0.001}} = $0.001 per request
562550
# Global: {"global": 0.05} = 5% global margin on all providers
@@ -1457,9 +1445,9 @@ def add_known_models(model_cost_map: Optional[Dict] = None):
14571445
from .types.llms.custom_llm import CustomLLMItem
14581446

14591447
custom_provider_map: List[CustomLLMItem] = []
1460-
_custom_providers: List[str] = (
1461-
[]
1462-
) # internal helper util, used to track names of custom providers
1448+
_custom_providers: List[
1449+
str
1450+
] = [] # internal helper util, used to track names of custom providers
14631451
disable_hf_tokenizer_download: Optional[bool] = (
14641452
None # disable huggingface tokenizer download. Defaults to openai clk100
14651453
)

litellm/_redis.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ def _get_redis_client_logic(**env_overrides):
327327
**env_overrides,
328328
}
329329

330-
_startup_nodes: Optional[Union[str, list]] = redis_kwargs.get("startup_nodes", None) or get_secret( # type: ignore
330+
_startup_nodes: Optional[Union[str, list]] = redis_kwargs.get(
331+
"startup_nodes", None
332+
) or get_secret( # type: ignore
331333
"REDIS_CLUSTER_NODES"
332334
)
333335

@@ -338,7 +340,9 @@ def _get_redis_client_logic(**env_overrides):
338340
elif _startup_nodes is None:
339341
redis_kwargs.pop("startup_nodes", None)
340342

341-
_sentinel_nodes: Optional[Union[str, list]] = redis_kwargs.get("sentinel_nodes", None) or get_secret( # type: ignore
343+
_sentinel_nodes: Optional[Union[str, list]] = redis_kwargs.get(
344+
"sentinel_nodes", None
345+
) or get_secret( # type: ignore
342346
"REDIS_SENTINEL_NODES"
343347
)
344348

@@ -609,7 +613,8 @@ def get_redis_async_client(
609613

610614
# Create async RedisCluster with IAM token as password if available
611615
cluster_client = async_redis.RedisCluster(
612-
startup_nodes=new_startup_nodes, **cluster_kwargs # type: ignore
616+
startup_nodes=new_startup_nodes,
617+
**cluster_kwargs, # type: ignore
613618
)
614619

615620
return cluster_client

litellm/assistants/main.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ def get_assistants(
184184
response=httpx.Response(
185185
status_code=400,
186186
content="Unsupported provider",
187-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
187+
request=httpx.Request(
188+
method="create_thread", url="https://github.com/BerriAI/litellm"
189+
), # type: ignore
188190
),
189191
)
190192

@@ -198,7 +200,9 @@ def get_assistants(
198200
response=httpx.Response(
199201
status_code=400,
200202
content="Unsupported provider",
201-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
203+
request=httpx.Request(
204+
method="create_thread", url="https://github.com/BerriAI/litellm"
205+
), # type: ignore
202206
),
203207
)
204208

@@ -394,7 +398,9 @@ def create_assistants(
394398
response=httpx.Response(
395399
status_code=400,
396400
content="Unsupported provider",
397-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
401+
request=httpx.Request(
402+
method="create_thread", url="https://github.com/BerriAI/litellm"
403+
), # type: ignore
398404
),
399405
)
400406
if response is None:
@@ -761,7 +767,9 @@ def create_thread(
761767
response=httpx.Response(
762768
status_code=400,
763769
content="Unsupported provider",
764-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
770+
request=httpx.Request(
771+
method="create_thread", url="https://github.com/BerriAI/litellm"
772+
), # type: ignore
765773
),
766774
)
767775
return response # type: ignore
@@ -916,7 +924,9 @@ def get_thread(
916924
response=httpx.Response(
917925
status_code=400,
918926
content="Unsupported provider",
919-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
927+
request=httpx.Request(
928+
method="create_thread", url="https://github.com/BerriAI/litellm"
929+
), # type: ignore
920930
),
921931
)
922932
return response # type: ignore
@@ -1103,7 +1113,9 @@ def add_message(
11031113
response=httpx.Response(
11041114
status_code=400,
11051115
content="Unsupported provider",
1106-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
1116+
request=httpx.Request(
1117+
method="create_thread", url="https://github.com/BerriAI/litellm"
1118+
), # type: ignore
11071119
),
11081120
)
11091121

@@ -1263,7 +1275,9 @@ def get_messages(
12631275
response=httpx.Response(
12641276
status_code=400,
12651277
content="Unsupported provider",
1266-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
1278+
request=httpx.Request(
1279+
method="create_thread", url="https://github.com/BerriAI/litellm"
1280+
), # type: ignore
12671281
),
12681282
)
12691283

@@ -1478,7 +1492,9 @@ def run_thread(
14781492
response=httpx.Response(
14791493
status_code=400,
14801494
content="Unsupported provider",
1481-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
1495+
request=httpx.Request(
1496+
method="create_thread", url="https://github.com/BerriAI/litellm"
1497+
), # type: ignore
14821498
),
14831499
)
14841500
return response # type: ignore

litellm/assistants/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ def _check_valid_arg(supported_params):
7171
if custom_llm_provider == "openai":
7272
optional_params = non_default_params
7373
elif custom_llm_provider == "azure":
74-
supported_params = (
75-
litellm.AzureOpenAIAssistantsAPIConfig().get_supported_openai_create_message_params()
76-
)
74+
supported_params = litellm.AzureOpenAIAssistantsAPIConfig().get_supported_openai_create_message_params()
7775
_check_valid_arg(supported_params=supported_params)
7876
optional_params = litellm.AzureOpenAIAssistantsAPIConfig().map_openai_params_create_message_params(
7977
non_default_params=non_default_params, optional_params=optional_params

litellm/batches/main.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ def create_batch(
359359
response=httpx.Response(
360360
status_code=400,
361361
content="Unsupported provider",
362-
request=httpx.Request(method="create_batch", url="https://github.com/BerriAI/litellm"), # type: ignore
362+
request=httpx.Request(
363+
method="create_batch", url="https://github.com/BerriAI/litellm"
364+
), # type: ignore
363365
),
364366
)
365367
return response
@@ -553,7 +555,9 @@ def _handle_retrieve_batch_providers_without_provider_config(
553555
response=httpx.Response(
554556
status_code=400,
555557
content="Unsupported provider",
556-
request=httpx.Request(method="retrieve_batch", url="https://github.com/BerriAI/litellm"), # type: ignore
558+
request=httpx.Request(
559+
method="retrieve_batch", url="https://github.com/BerriAI/litellm"
560+
), # type: ignore
557561
),
558562
)
559563
return response
@@ -819,7 +823,11 @@ def list_batches(
819823
max_retries=optional_params.max_retries,
820824
)
821825
elif custom_llm_provider == "azure":
822-
api_base = optional_params.api_base or litellm.api_base or get_secret_str("AZURE_API_BASE") # type: ignore
826+
api_base = (
827+
optional_params.api_base
828+
or litellm.api_base
829+
or get_secret_str("AZURE_API_BASE")
830+
) # type: ignore
823831
api_version = (
824832
optional_params.api_version
825833
or litellm.api_version
@@ -887,7 +895,9 @@ def list_batches(
887895
response=httpx.Response(
888896
status_code=400,
889897
content="Unsupported provider",
890-
request=httpx.Request(method="create_thread", url="https://github.com/BerriAI/litellm"), # type: ignore
898+
request=httpx.Request(
899+
method="create_thread", url="https://github.com/BerriAI/litellm"
900+
), # type: ignore
891901
),
892902
)
893903
return response
@@ -1097,7 +1107,9 @@ def cancel_batch(
10971107
response=httpx.Response(
10981108
status_code=400,
10991109
content="Unsupported provider",
1100-
request=httpx.Request(method="cancel_batch", url="https://github.com/BerriAI/litellm"), # type: ignore
1110+
request=httpx.Request(
1111+
method="cancel_batch", url="https://github.com/BerriAI/litellm"
1112+
), # type: ignore
11011113
),
11021114
)
11031115
return response

litellm/budget_manager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ def load_data(self):
6767
)
6868
response = response.json()
6969
if response["status"] == "error":
70-
self.user_dict = (
71-
{}
72-
) # assume this means the user dict hasn't been stored yet
70+
self.user_dict = {} # assume this means the user dict hasn't been stored yet
7371
else:
7472
self.user_dict = response["data"]
7573

litellm/caching/caching_handler.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ class CachingHandlerResponse(BaseModel):
7979

8080
cached_result: Optional[Any] = None
8181
final_embedding_cached_response: Optional[EmbeddingResponse] = None
82-
embedding_all_elements_cache_hit: bool = (
83-
False # this is set to True when all elements in the list have a cache hit in the embedding cache, if true return the final_embedding_cached_response no need to make an API call
84-
)
82+
embedding_all_elements_cache_hit: bool = False # this is set to True when all elements in the list have a cache hit in the embedding cache, if true return the final_embedding_cached_response no need to make an API call
8583

8684

8785
in_memory_cache_obj = InMemoryCache()
@@ -165,10 +163,11 @@ async def _async_get_cache(
165163
"""
166164
# Check if caching should be performed BEFORE doing expensive operations
167165
if (
168-
(kwargs.get("caching", None) is None and litellm.cache is not None)
169-
or kwargs.get("caching", False) is True
170-
) and (
171-
kwargs.get("cache", {}).get("no-cache", False) is not True
166+
(
167+
(kwargs.get("caching", None) is None and litellm.cache is not None)
168+
or kwargs.get("caching", False) is True
169+
)
170+
and (kwargs.get("cache", {}).get("no-cache", False) is not True)
172171
): # allow users to control returning cached responses from the completion function
173172
args = args or ()
174173
final_embedding_cached_response: Optional[EmbeddingResponse] = None

litellm/caching/redis_cluster_cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ async def test_connection(self) -> dict:
7979

8080
# Create a fresh Redis Cluster client with current settings
8181
redis_client = redis_async.RedisCluster(
82-
startup_nodes=new_startup_nodes, **cluster_kwargs # type: ignore
82+
startup_nodes=new_startup_nodes,
83+
**cluster_kwargs, # type: ignore
8384
)
8485

8586
# Test the connection

0 commit comments

Comments
 (0)