refactor(project): make pyright & ruff happy

This commit is contained in:
MingxuanGame
2025-08-22 08:21:52 +00:00
parent 3b1d7a2234
commit 598fcc8b38
157 changed files with 2382 additions and 4590 deletions

View File

@@ -26,7 +26,7 @@ async def create_oauth_app(
redirect_uris: list[str] = Body(..., description="允许的重定向 URI 列表"),
current_user: User = Security(get_client_user),
):
result = await session.execute( # pyright: ignore[reportDeprecated]
result = await session.execute(
text(
"SELECT AUTO_INCREMENT FROM information_schema.TABLES "
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oauth_clients'"
@@ -84,9 +84,7 @@ async def get_user_oauth_apps(
session: Database,
current_user: User = Security(get_client_user),
):
oauth_apps = await session.exec(
select(OAuthClient).where(OAuthClient.owner_id == current_user.id)
)
oauth_apps = await session.exec(select(OAuthClient).where(OAuthClient.owner_id == current_user.id))
return [
{
"name": app.name,
@@ -113,13 +111,9 @@ async def delete_oauth_app(
if not oauth_client:
raise HTTPException(status_code=404, detail="OAuth app not found")
if oauth_client.owner_id != current_user.id:
raise HTTPException(
status_code=403, detail="Forbidden: Not the owner of this app"
)
raise HTTPException(status_code=403, detail="Forbidden: Not the owner of this app")
tokens = await session.exec(
select(OAuthToken).where(OAuthToken.client_id == client_id)
)
tokens = await session.exec(select(OAuthToken).where(OAuthToken.client_id == client_id))
for token in tokens:
await session.delete(token)
@@ -144,9 +138,7 @@ async def update_oauth_app(
if not oauth_client:
raise HTTPException(status_code=404, detail="OAuth app not found")
if oauth_client.owner_id != current_user.id:
raise HTTPException(
status_code=403, detail="Forbidden: Not the owner of this app"
)
raise HTTPException(status_code=403, detail="Forbidden: Not the owner of this app")
oauth_client.name = name
oauth_client.description = description
@@ -176,14 +168,10 @@ async def refresh_secret(
if not oauth_client:
raise HTTPException(status_code=404, detail="OAuth app not found")
if oauth_client.owner_id != current_user.id:
raise HTTPException(
status_code=403, detail="Forbidden: Not the owner of this app"
)
raise HTTPException(status_code=403, detail="Forbidden: Not the owner of this app")
oauth_client.client_secret = secrets.token_hex()
tokens = await session.exec(
select(OAuthToken).where(OAuthToken.client_id == client_id)
)
tokens = await session.exec(select(OAuthToken).where(OAuthToken.client_id == client_id))
for token in tokens:
await session.delete(token)
@@ -215,9 +203,7 @@ async def generate_oauth_code(
raise HTTPException(status_code=404, detail="OAuth app not found")
if redirect_uri not in client.redirect_uris:
raise HTTPException(
status_code=403, detail="Redirect URI not allowed for this client"
)
raise HTTPException(status_code=403, detail="Redirect URI not allowed for this client")
code = secrets.token_urlsafe(80)
await redis.hset( # pyright: ignore[reportGeneralTypeIssues]

View File

@@ -50,12 +50,8 @@ async def check_user_relationship(
)
).first()
is_followed = bool(
target_relationship and target_relationship.type == RelationshipType.FOLLOW
)
is_following = bool(
my_relationship and my_relationship.type == RelationshipType.FOLLOW
)
is_followed = bool(target_relationship and target_relationship.type == RelationshipType.FOLLOW)
is_following = bool(my_relationship and my_relationship.type == RelationshipType.FOLLOW)
return CheckResponse(
is_followed=is_followed,

View File

@@ -40,16 +40,13 @@ async def create_team(
支持的图片格式: PNG、JPEG、GIF
"""
user_id = current_user.id
assert user_id
if (await current_user.awaitable_attrs.team_membership) is not None:
raise HTTPException(status_code=403, detail="You are already in a team")
is_existed = (await session.exec(select(exists()).where(Team.name == name))).first()
if is_existed:
raise HTTPException(status_code=409, detail="Name already exists")
is_existed = (
await session.exec(select(exists()).where(Team.short_name == short_name))
).first()
is_existed = (await session.exec(select(exists()).where(Team.short_name == short_name))).first()
if is_existed:
raise HTTPException(status_code=409, detail="Short name already exists")
@@ -101,7 +98,6 @@ async def update_team(
"""
team = await session.get(Team, team_id)
user_id = current_user.id
assert user_id
if not team:
raise HTTPException(status_code=404, detail="Team not found")
if team.leader_id != user_id:
@@ -110,9 +106,7 @@ async def update_team(
is_existed = (await session.exec(select(exists()).where(Team.name == name))).first()
if is_existed:
raise HTTPException(status_code=409, detail="Name already exists")
is_existed = (
await session.exec(select(exists()).where(Team.short_name == short_name))
).first()
is_existed = (await session.exec(select(exists()).where(Team.short_name == short_name))).first()
if is_existed:
raise HTTPException(status_code=409, detail="Short name already exists")
@@ -132,20 +126,12 @@ async def update_team(
team.cover_url = await storage.get_file_url(storage_path)
if leader_id is not None:
if not (
await session.exec(select(exists()).where(User.id == leader_id))
).first():
if not (await session.exec(select(exists()).where(User.id == leader_id))).first():
raise HTTPException(status_code=404, detail="Leader not found")
if not (
await session.exec(
select(TeamMember).where(
TeamMember.user_id == leader_id, TeamMember.team_id == team.id
)
)
await session.exec(select(TeamMember).where(TeamMember.user_id == leader_id, TeamMember.team_id == team.id))
).first():
raise HTTPException(
status_code=404, detail="Leader is not a member of the team"
)
raise HTTPException(status_code=404, detail="Leader is not a member of the team")
team.leader_id = leader_id
await session.commit()
@@ -166,9 +152,7 @@ async def delete_team(
if team.leader_id != current_user.id:
raise HTTPException(status_code=403, detail="You are not the team leader")
team_members = await session.exec(
select(TeamMember).where(TeamMember.team_id == team_id)
)
team_members = await session.exec(select(TeamMember).where(TeamMember.team_id == team_id))
for member in team_members:
await session.delete(member)
@@ -186,15 +170,10 @@ async def get_team(
session: Database,
team_id: int = Path(..., description="战队 ID"),
):
members = (
await session.exec(select(TeamMember).where(TeamMember.team_id == team_id))
).all()
members = (await session.exec(select(TeamMember).where(TeamMember.team_id == team_id))).all()
return TeamQueryResp(
team=members[0].team,
members=[
await UserResp.from_db(m.user, session, include=BASE_INCLUDES)
for m in members
],
members=[await UserResp.from_db(m.user, session, include=BASE_INCLUDES) for m in members],
)
@@ -213,15 +192,11 @@ async def request_join_team(
if (
await session.exec(
select(exists()).where(
TeamRequest.team_id == team_id, TeamRequest.user_id == current_user.id
)
select(exists()).where(TeamRequest.team_id == team_id, TeamRequest.user_id == current_user.id)
)
).first():
raise HTTPException(status_code=409, detail="Join request already exists")
team_request = TeamRequest(
user_id=current_user.id, team_id=team_id, requested_at=datetime.now(UTC)
)
team_request = TeamRequest(user_id=current_user.id, team_id=team_id, requested_at=datetime.now(UTC))
session.add(team_request)
await session.commit()
await session.refresh(team_request)
@@ -229,9 +204,7 @@ async def request_join_team(
@router.post("/team/{team_id}/{user_id}/request", name="接受加入请求", status_code=204)
@router.delete(
"/team/{team_id}/{user_id}/request", name="拒绝加入请求", status_code=204
)
@router.delete("/team/{team_id}/{user_id}/request", name="拒绝加入请求", status_code=204)
async def handle_request(
req: Request,
session: Database,
@@ -247,11 +220,7 @@ async def handle_request(
raise HTTPException(status_code=403, detail="You are not the team leader")
team_request = (
await session.exec(
select(TeamRequest).where(
TeamRequest.team_id == team_id, TeamRequest.user_id == user_id
)
)
await session.exec(select(TeamRequest).where(TeamRequest.team_id == team_id, TeamRequest.user_id == user_id))
).first()
if not team_request:
raise HTTPException(status_code=404, detail="Join request not found")
@@ -261,16 +230,10 @@ async def handle_request(
raise HTTPException(status_code=404, detail="User not found")
if req.method == "POST":
if (
await session.exec(select(exists()).where(TeamMember.user_id == user_id))
).first():
raise HTTPException(
status_code=409, detail="User is already a member of the team"
)
if (await session.exec(select(exists()).where(TeamMember.user_id == user_id))).first():
raise HTTPException(status_code=409, detail="User is already a member of the team")
session.add(
TeamMember(user_id=user_id, team_id=team_id, joined_at=datetime.now(UTC))
)
session.add(TeamMember(user_id=user_id, team_id=team_id, joined_at=datetime.now(UTC)))
await server.new_private_notification(TeamApplicationAccept.init(team_request))
else:
@@ -294,19 +257,13 @@ async def kick_member(
raise HTTPException(status_code=403, detail="You are not the team leader")
team_member = (
await session.exec(
select(TeamMember).where(
TeamMember.team_id == team_id, TeamMember.user_id == user_id
)
)
await session.exec(select(TeamMember).where(TeamMember.team_id == team_id, TeamMember.user_id == user_id))
).first()
if not team_member:
raise HTTPException(status_code=404, detail="User is not a member of the team")
if team.leader_id == current_user.id:
raise HTTPException(
status_code=403, detail="You cannot leave because you are the team leader"
)
raise HTTPException(status_code=403, detail="You cannot leave because you are the team leader")
await session.delete(team_member)
await session.commit()

View File

@@ -35,10 +35,7 @@ async def user_rename(
返回:
- 成功: None
"""
assert current_user is not None
samename_user = (
await session.exec(select(User).where(User.username == new_name))
).first()
samename_user = (await session.exec(select(User).where(User.username == new_name))).first()
if samename_user:
raise HTTPException(409, "Username Exisits")
errors = validate_username(new_name)