chore: 升级到 Python 3.14 并现代化代码

- 升级 Docker 基础镜像从 Python 3.12 到 3.14
- 更新 pyproject.toml 支持 Python 3.13/3.14
- 移除 Python 3.8/3.9/3.10/3.11 分类器
- 更新 black 和 mypy 配置目标版本
- 将 get_event_loop() 替换为 get_running_loop() 加上 RuntimeError 处理
- 简化 compute_cost_sync 中的 asyncio.run 使用
- Dict/List/Tuple/Set → dict/list/tuple/set (PEP 585)
- Optional[T] → T | None (PEP 604)
- Union[A, B] → A | B (PEP 604)
- 移除废弃的 typing 导入
- 移除不必要的字符串引号注解
This commit is contained in:
AAEE86
2026-01-30 03:10:21 +08:00
parent 3e75bc8964
commit 24d24f6829
255 changed files with 4062 additions and 4173 deletions

View File

@@ -8,7 +8,7 @@
import hashlib
import time
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING
from starlette.requests import Request
@@ -113,7 +113,7 @@ class PluginMiddleware:
await send(message)
# 3. 调用下游应用
exception_occurred: Optional[Exception] = None
exception_occurred: Exception | None = None
try:
await self.app(scope, receive, send_wrapper)
except Exception as e:
@@ -160,7 +160,7 @@ class PluginMiddleware:
def _finalize_db_session(
self,
db: "Session",
db: Session,
*,
should_commit: bool,
should_rollback: bool,
@@ -196,7 +196,7 @@ class PluginMiddleware:
logger.debug(f"{log_prefix}关闭数据库连接时出错(可忽略): {close_error}")
async def _cleanup_db_session(
self, request: Request, exception: Optional[Exception]
self, request: Request, exception: Exception | None
) -> None:
"""清理数据库会话
@@ -293,7 +293,7 @@ class PluginMiddleware:
async def _get_rate_limit_key_and_config(
self, request: Request
) -> tuple[Optional[str], Optional[int]]:
) -> tuple[str | None, int | None]:
"""
获取速率限制的key和配置
@@ -344,7 +344,7 @@ class PluginMiddleware:
# 其他端点不应用速率限制(或已在 skip_rate_limit_paths 中跳过)
return None, None
async def _call_rate_limit_plugins(self, request: Request) -> Optional[RateLimitResult]:
async def _call_rate_limit_plugins(self, request: Request) -> RateLimitResult | None:
"""调用限流插件"""
# 跳过不需要限流的路径(支持前缀匹配)

View File

@@ -5,7 +5,7 @@
"""
from dataclasses import dataclass
from typing import Dict, Literal, Optional
from typing import Literal
RateLimitScope = Literal["server_ip", "user", "api_key", "skip"]
@@ -27,7 +27,7 @@ class RateLimitConfig:
"""
# 默认策略配置
POLICIES: Dict[str, RateLimitPolicy] = {
POLICIES: dict[str, RateLimitPolicy] = {
# 客户端 API 端点 - 服务器级别 IP 限制
"/v1/": RateLimitPolicy(
scope="server_ip", limit=60, description="Claude/OpenAI API 端点,服务器级别限制"
@@ -49,7 +49,7 @@ class RateLimitConfig:
}
@classmethod
def get_policy_for_path(cls, path: str) -> Optional[RateLimitPolicy]:
def get_policy_for_path(cls, path: str) -> RateLimitPolicy | None:
"""
根据路径获取速率限制策略
@@ -82,6 +82,6 @@ class RateLimitConfig:
cls.POLICIES[prefix] = policy
@classmethod
def get_all_policies(cls) -> Dict[str, RateLimitPolicy]:
def get_all_policies(cls) -> dict[str, RateLimitPolicy]:
"""获取所有策略配置"""
return cls.POLICIES.copy()