mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
fix: 修复 mypy 类型检查错误并升级到 Python 3.14
主要变更: - 修复 1483 个 mypy 类型检查错误 - 添加缺失的类型注解 (Any, Callable, Session 等) - 修复隐式 Optional 类型 (param: Type = None -> param: Type | None = None) - 修复 __new__ 单例模式返回类型 - 添加 type: ignore 注释处理第三方库类型问题 - 更新 pyproject.toml 依赖到 Python 3.14 兼容版本 - 更新 mypy/black 配置为 Python 3.14
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
提供统一的用户认证和授权功能
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
import hashlib
|
||||
|
||||
from fastapi import Depends, Header, HTTPException, status
|
||||
@@ -183,7 +186,7 @@ def require_admin(current_user: User = Depends(get_current_user)) -> User:
|
||||
return current_user
|
||||
|
||||
|
||||
def require_role(required_role: UserRole):
|
||||
def require_role(required_role: UserRole) -> Any:
|
||||
"""
|
||||
要求特定角色权限的装饰器工厂
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ def cache_result(key_prefix: str, ttl: int = 60, user_specific: bool = True) ->
|
||||
|
||||
def decorator(func: Callable) -> Callable:
|
||||
@functools.wraps(func)
|
||||
async def wrapper(*args, **kwargs) -> Any:
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
redis_client = get_redis_client_sync()
|
||||
|
||||
# 如果 Redis 不可用,直接执行原函数
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
"""
|
||||
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import Request
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
"""
|
||||
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
import os
|
||||
import pathlib
|
||||
import uuid
|
||||
@@ -29,7 +32,7 @@ class StartupTaskCoordinator:
|
||||
# 注意:这在 fork 模式下每个 worker 都是独立的
|
||||
_startup_cleanup_attempted = False
|
||||
|
||||
def __init__(self, redis_client=None, lock_dir: str | None = None):
|
||||
def __init__(self, redis_client: Any | None = None, lock_dir: str | None = None) -> None:
|
||||
self.redis = redis_client
|
||||
self._tokens: dict[str, str] = {}
|
||||
self._file_handles: dict[str, object] = {}
|
||||
@@ -98,7 +101,7 @@ class StartupTaskCoordinator:
|
||||
|
||||
return await self._acquire_file_lock(name)
|
||||
|
||||
async def release(self, name: str):
|
||||
async def release(self, name: str) -> Any:
|
||||
if self.redis and name in self._tokens:
|
||||
token = self._tokens.pop(name)
|
||||
script = """
|
||||
@@ -140,7 +143,7 @@ class StartupTaskCoordinator:
|
||||
return False
|
||||
|
||||
|
||||
async def ensure_singleton_task(name: str, redis_client=None, ttl: int | None = None):
|
||||
async def ensure_singleton_task(name: str, redis_client: Any | None = None, ttl: int | None = None) -> Any:
|
||||
"""便捷协程,返回 (coordinator, acquired)"""
|
||||
|
||||
coordinator = StartupTaskCoordinator(redis_client)
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
为异步函数和操作提供超时保护
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from functools import wraps
|
||||
from typing import Any, TypeVar
|
||||
@@ -25,7 +27,7 @@ class AsyncTimeoutError(TimeoutError):
|
||||
self.timeout = timeout
|
||||
|
||||
|
||||
def with_timeout(seconds: float, operation_name: str | None = None):
|
||||
def with_timeout(seconds: float, operation_name: str | None = None) -> Any:
|
||||
"""
|
||||
装饰器:为异步函数添加超时保护
|
||||
|
||||
@@ -45,7 +47,7 @@ def with_timeout(seconds: float, operation_name: str | None = None):
|
||||
|
||||
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||
@wraps(func)
|
||||
async def wrapper(*args, **kwargs):
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
op_name = operation_name or func.__name__
|
||||
try:
|
||||
return await asyncio.wait_for(func(*args, **kwargs), timeout=seconds)
|
||||
@@ -63,10 +65,10 @@ def with_timeout(seconds: float, operation_name: str | None = None):
|
||||
|
||||
|
||||
async def run_with_timeout(
|
||||
coro,
|
||||
coro: Any,
|
||||
timeout: float,
|
||||
operation_name: str = "operation",
|
||||
default: T = None,
|
||||
default: T | None = None,
|
||||
raise_on_timeout: bool = True,
|
||||
) -> T:
|
||||
"""
|
||||
@@ -117,16 +119,16 @@ class TimeoutContext:
|
||||
self.operation_name = operation_name
|
||||
self._task: asyncio.Task | None = None
|
||||
|
||||
async def __aenter__(self):
|
||||
async def __aenter__(self) -> None:
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
||||
# asyncio.timeout 在 Python 3.11+ 可用
|
||||
# 这里使用更通用的方式
|
||||
pass
|
||||
|
||||
|
||||
async def with_timeout_context(timeout: float, operation_name: str = "operation"):
|
||||
async def with_timeout_context(timeout: float, operation_name: str = "operation") -> Any:
|
||||
"""
|
||||
超时上下文管理器(Python 3.11+ asyncio.timeout 的替代)
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
支持同步和异步函数
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import inspect
|
||||
from contextlib import contextmanager
|
||||
@@ -25,7 +27,7 @@ class TransactionError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _find_db_session(args, kwargs) -> Session | None:
|
||||
def _find_db_session(args: Any, kwargs: Any) -> Session | None:
|
||||
"""从参数中查找数据库会话"""
|
||||
# 从位置参数中查找Session
|
||||
for arg in args:
|
||||
@@ -40,7 +42,7 @@ def _find_db_session(args, kwargs) -> Session | None:
|
||||
return None
|
||||
|
||||
|
||||
def transactional(commit: bool = True, rollback_on_error: bool = True):
|
||||
def transactional(commit: bool = True, rollback_on_error: bool = True) -> Any:
|
||||
"""
|
||||
事务装饰器,支持同步和异步函数
|
||||
|
||||
@@ -64,7 +66,7 @@ def transactional(commit: bool = True, rollback_on_error: bool = True):
|
||||
# 检查是否是异步函数
|
||||
if inspect.iscoroutinefunction(func):
|
||||
@functools.wraps(func)
|
||||
async def async_wrapper(*args, **kwargs) -> Any:
|
||||
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
db_session = _find_db_session(args, kwargs)
|
||||
|
||||
if not db_session:
|
||||
@@ -106,7 +108,7 @@ def transactional(commit: bool = True, rollback_on_error: bool = True):
|
||||
return async_wrapper
|
||||
else:
|
||||
@functools.wraps(func)
|
||||
def sync_wrapper(*args, **kwargs) -> Any:
|
||||
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
db_session = _find_db_session(args, kwargs)
|
||||
|
||||
if not db_session:
|
||||
@@ -197,7 +199,7 @@ def transaction_scope(
|
||||
raise
|
||||
|
||||
|
||||
def retry_on_database_error(max_retries: int = 3, delay: float = 0.1):
|
||||
def retry_on_database_error(max_retries: int = 3, delay: float = 0.1) -> Any:
|
||||
"""
|
||||
数据库错误重试装饰器
|
||||
|
||||
@@ -208,7 +210,7 @@ def retry_on_database_error(max_retries: int = 3, delay: float = 0.1):
|
||||
|
||||
def decorator(func: Callable) -> Callable:
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs) -> Any:
|
||||
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
import random
|
||||
import time
|
||||
|
||||
@@ -246,7 +248,7 @@ class BatchOperation:
|
||||
self.operations = []
|
||||
self.operation_count = 0
|
||||
|
||||
def add(self, obj):
|
||||
def add(self, obj: Any) -> Any:
|
||||
"""添加对象到批处理"""
|
||||
self.operations.append(("add", obj))
|
||||
self.operation_count += 1
|
||||
@@ -254,7 +256,7 @@ class BatchOperation:
|
||||
if self.operation_count >= self.batch_size:
|
||||
self.flush()
|
||||
|
||||
def update(self, obj):
|
||||
def update(self, obj: Any) -> Any:
|
||||
"""添加更新操作到批处理"""
|
||||
self.operations.append(("merge", obj))
|
||||
self.operation_count += 1
|
||||
@@ -262,7 +264,7 @@ class BatchOperation:
|
||||
if self.operation_count >= self.batch_size:
|
||||
self.flush()
|
||||
|
||||
def flush(self):
|
||||
def flush(self) -> Any:
|
||||
"""执行当前批次的所有操作"""
|
||||
if not self.operations:
|
||||
return
|
||||
@@ -288,16 +290,16 @@ class BatchOperation:
|
||||
self.operations.clear()
|
||||
self.operation_count = 0
|
||||
|
||||
def commit(self):
|
||||
def commit(self) -> Any:
|
||||
"""提交所有操作"""
|
||||
self.flush() # 确保所有操作都已flush
|
||||
self.db.commit()
|
||||
logger.debug("批量操作提交完成")
|
||||
|
||||
def __enter__(self):
|
||||
def __enter__(self) -> None:
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
||||
if exc_type is None:
|
||||
# 正常退出,提交事务
|
||||
self.commit()
|
||||
|
||||
Reference in New Issue
Block a user