chore(linter): update ruff rules

This commit is contained in:
MingxuanGame
2025-10-03 15:46:53 +00:00
parent b10425ad91
commit d490239f46
59 changed files with 393 additions and 425 deletions

View File

@@ -12,7 +12,7 @@ from app.service.asset_proxy_service import get_asset_proxy_service
from fastapi import Request
async def process_response_assets(data: Any, request: Request) -> Any:
async def process_response_assets(data: Any) -> Any:
"""
根据配置处理响应数据中的资源URL
@@ -72,7 +72,7 @@ def asset_proxy_response(func):
# 如果有request对象且启用了资源代理则处理响应
if request and settings.enable_asset_proxy and should_process_asset_proxy(request.url.path):
result = await process_response_assets(result, request)
result = await process_response_assets(result)
return result

View File

@@ -113,6 +113,7 @@ class BeatmapCacheService:
if size:
total_size += size
except Exception:
logger.debug(f"Failed to get size for key {key}")
continue
return {

View File

@@ -36,11 +36,8 @@ def safe_json_dumps(data) -> str:
def generate_hash(data) -> str:
"""生成数据的MD5哈希值"""
if isinstance(data, str):
content = data
else:
content = safe_json_dumps(data)
return hashlib.md5(content.encode()).hexdigest()
content = data if isinstance(data, str) else safe_json_dumps(data)
return hashlib.md5(content.encode(), usedforsecurity=False).hexdigest()
class BeatmapsetCacheService:

View File

@@ -110,9 +110,7 @@ class ProcessingBeatmapset:
changed_beatmaps = []
for bm in self.beatmapset.beatmaps:
saved = next((s for s in self.record.beatmaps if s["beatmap_id"] == bm.id), None)
if not saved:
changed_beatmaps.append(ChangedBeatmap(bm.id, BeatmapChangeType.MAP_ADDED))
elif saved["is_deleted"]:
if not saved or saved["is_deleted"]:
changed_beatmaps.append(ChangedBeatmap(bm.id, BeatmapChangeType.MAP_ADDED))
elif saved["md5"] != bm.checksum:
changed_beatmaps.append(ChangedBeatmap(bm.id, BeatmapChangeType.MAP_UPDATED))
@@ -285,7 +283,7 @@ class BeatmapsetUpdateService:
async def _process_changed_beatmapset(self, beatmapset: BeatmapsetResp):
async with with_db() as session:
db_beatmapset = await session.get(Beatmapset, beatmapset.id)
new_beatmapset = await Beatmapset.from_resp_no_save(session, beatmapset)
new_beatmapset = await Beatmapset.from_resp_no_save(beatmapset)
if db_beatmapset:
await session.merge(new_beatmapset)
await session.commit()
@@ -356,5 +354,7 @@ def init_beatmapset_update_service(fetcher: "Fetcher") -> BeatmapsetUpdateServic
def get_beatmapset_update_service() -> BeatmapsetUpdateService:
if service is None:
raise ValueError("BeatmapsetUpdateService is not initialized")
assert service is not None, "BeatmapsetUpdateService is not initialized"
return service

View File

@@ -128,7 +128,11 @@ class LoginLogService:
login_success=False,
login_method=login_method,
user_agent=user_agent,
notes=f"Failed login attempt: {attempted_username}" if attempted_username else "Failed login attempt",
notes=(
f"Failed login attempt on user {attempted_username}: {notes}"
if attempted_username
else "Failed login attempt"
),
)

View File

@@ -120,7 +120,7 @@ class PasswordResetService:
await redis.delete(reset_code_key)
await redis.delete(rate_limit_key)
except Exception:
pass
logger.warning("Failed to clean up Redis data after error")
logger.exception("Redis operation failed")
return False, "服务暂时不可用,请稍后重试"

View File

@@ -593,10 +593,7 @@ class RankingCacheService:
async def invalidate_country_cache(self, ruleset: GameMode | None = None) -> None:
"""使地区排行榜缓存失效"""
try:
if ruleset:
pattern = f"country_ranking:{ruleset}:*"
else:
pattern = "country_ranking:*"
pattern = f"country_ranking:{ruleset}:*" if ruleset else "country_ranking:*"
keys = await self.redis.keys(pattern)
if keys:
@@ -608,10 +605,7 @@ class RankingCacheService:
async def invalidate_team_cache(self, ruleset: GameMode | None = None) -> None:
"""使战队排行榜缓存失效"""
try:
if ruleset:
pattern = f"team_ranking:{ruleset}:*"
else:
pattern = "team_ranking:*"
pattern = f"team_ranking:{ruleset}:*" if ruleset else "team_ranking:*"
keys = await self.redis.keys(pattern)
if keys:
@@ -637,6 +631,7 @@ class RankingCacheService:
if size:
total_size += size
except Exception:
logger.warning(f"Failed to get memory usage for key {key}")
continue
return {

View File

View File

@@ -35,19 +35,19 @@ class ChatSubscriber(RedisSubscriber):
self.add_handler(ON_NOTIFICATION, self.on_notification)
self.start()
async def on_join_room(self, c: str, s: str):
async def on_join_room(self, c: str, s: str): # noqa: ARG002
channel_id, user_id = s.split(":")
if self.chat_server is None:
return
await self.chat_server.join_room_channel(int(channel_id), int(user_id))
async def on_leave_room(self, c: str, s: str):
async def on_leave_room(self, c: str, s: str): # noqa: ARG002
channel_id, user_id = s.split(":")
if self.chat_server is None:
return
await self.chat_server.leave_room_channel(int(channel_id), int(user_id))
async def on_notification(self, c: str, s: str):
async def on_notification(self, c: str, s: str): # noqa: ARG002
try:
detail = TypeAdapter(NotificationDetails).validate_json(s)
except ValueError:

View File

@@ -357,6 +357,7 @@ class UserCacheService:
if size:
total_size += size
except Exception:
logger.warning(f"Failed to get memory usage for key {key}")
continue
return {

View File

@@ -288,10 +288,6 @@ This email was sent automatically, please do not reply.
redis: Redis,
user_id: int,
code: str,
ip_address: str | None = None,
user_agent: str | None = None,
client_id: int | None = None,
country_code: str | None = None,
) -> tuple[bool, str]:
"""验证邮箱验证码"""
try: