refactor(types): declare subscription identity query results

This commit is contained in:
jxxghp
2026-10-16 20:28:06 +08:00
parent 0e9e09c150
commit 94aeac973a
5 changed files with 41 additions and 36 deletions

View File

@@ -1,8 +1,8 @@
import time
from typing import Any, Optional
from typing import Any, Optional, Self
from uuid import uuid4
from sqlalchemy import JSON, Float, Index, Integer, String, or_, select
from sqlalchemy import JSON, ColumnElement, Float, Index, Integer, String, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Mapped, Session, mapped_column
@@ -133,10 +133,10 @@ class Subscribe(Base):
@classmethod
def _identity_condition(
cls,
media_source: Optional[MediaSource] = None,
media_source: MediaSource | str | None = None,
media_id: Optional[str] = None,
music_type: Optional[str] = None,
):
) -> Optional[ColumnElement[bool]]:
"""按统一媒体身份优先级构造订阅查询条件。"""
if not media_source or media_id is None or not str(media_id).strip():
return None
@@ -156,7 +156,7 @@ class Subscribe(Base):
season: Optional[int] = None,
episode_group: Optional[str] = None,
music_type: Optional[str] = None,
):
) -> Optional[Self]:
"""按媒体身份、季号与剧集组查询已有订阅。"""
condition = cls._identity_condition(media_source, media_id, music_type)
if condition is None:
@@ -164,7 +164,8 @@ class Subscribe(Base):
statement = select(cls).where(condition)
if season is not None:
statement = statement.where(cls.season == season)
return db.execute(statement.where(cls.episode_group == episode_group)).scalars().first()
subscription: Optional[Self] = db.execute(statement.where(cls.episode_group == episode_group)).scalars().first()
return subscription
@classmethod
async def async_exists(
@@ -175,7 +176,7 @@ class Subscribe(Base):
season: Optional[int] = None,
episode_group: Optional[str] = None,
music_type: Optional[str] = None,
):
) -> Optional[Self]:
"""异步按媒体身份、季号与剧集组查询已有订阅。"""
condition = cls._identity_condition(media_source, media_id, music_type)
if condition is None:
@@ -184,7 +185,8 @@ class Subscribe(Base):
if season is not None:
statement = statement.where(cls.season == season)
result = await db.execute(statement.where(cls.episode_group == episode_group))
return result.scalars().first()
subscription: Optional[Self] = result.scalars().first()
return subscription
@classmethod
def exists_by_username(
@@ -196,7 +198,7 @@ class Subscribe(Base):
season: Optional[int] = None,
episode_group: Optional[str] = None,
music_type: Optional[str] = None,
):
) -> Optional[Self]:
"""
按订阅 owner、媒体身份、季号与剧集组查询订阅行。
"""
@@ -208,7 +210,8 @@ class Subscribe(Base):
statement = select(cls).where(cls.username == username, condition)
if season is not None:
statement = statement.where(cls.season == season)
return db.execute(statement.where(cls.episode_group == episode_group)).scalars().first()
subscription: Optional[Self] = db.execute(statement.where(cls.episode_group == episode_group)).scalars().first()
return subscription
@classmethod
async def async_exists_by_username(
@@ -220,7 +223,7 @@ class Subscribe(Base):
season: Optional[int] = None,
episode_group: Optional[str] = None,
music_type: Optional[str] = None,
):
) -> Optional[Self]:
"""
异步按订阅 owner、媒体身份、季号与剧集组查询订阅行。
"""
@@ -233,10 +236,11 @@ class Subscribe(Base):
if season is not None:
statement = statement.where(cls.season == season)
result = await db.execute(statement.where(cls.episode_group == episode_group))
return result.scalars().first()
subscription: Optional[Self] = result.scalars().first()
return subscription
@classmethod
def get_by_state(cls, db: Session, state: str | None = None):
def get_by_state(cls, db: Session, state: str | None = None) -> list[Self]:
"""在调用方 Session 中按状态列表查询订阅。"""
statement = select(cls)
if state:
@@ -244,7 +248,7 @@ class Subscribe(Base):
return list(db.execute(statement).scalars().all())
@classmethod
async def async_get_by_state(cls, db: AsyncSession, state: str | None = None):
async def async_get_by_state(cls, db: AsyncSession, state: str | None = None) -> list[Self]:
"""在调用方 AsyncSession 中按状态列表查询订阅。"""
statement = select(cls)
if state:
@@ -258,12 +262,13 @@ class Subscribe(Base):
db: Session,
title: str,
season: Optional[int] = None,
):
) -> Optional[Self]:
"""在调用方 Session 中按标题查询订阅。"""
statement = select(cls).where(cls.name == title)
if season is not None:
statement = statement.where(cls.season == season)
return db.execute(statement).scalars().first()
subscription: Optional[Self] = db.execute(statement).scalars().first()
return subscription
@classmethod
async def async_get_by_title(
@@ -271,13 +276,14 @@ class Subscribe(Base):
db: AsyncSession,
title: str,
season: Optional[int] = None,
):
) -> Optional[Self]:
"""在调用方 AsyncSession 中按标题查询订阅。"""
statement = select(cls).where(cls.name == title)
if season is not None:
statement = statement.where(cls.season == season)
result = await db.execute(statement)
return result.scalars().first()
subscription: Optional[Self] = result.scalars().first()
return subscription
@classmethod
async def async_list_by_title(
@@ -285,7 +291,7 @@ class Subscribe(Base):
db: AsyncSession,
title: str,
season: Optional[int] = None,
):
) -> list[Self]:
"""在调用方 AsyncSession 中按标题查询候选订阅列表。"""
statement = select(cls).where(cls.name == title)
if season is not None:
@@ -300,7 +306,7 @@ class Subscribe(Base):
media_source: MediaSource | str | None = None,
media_id: str | None = None,
music_type: Optional[str] = None,
):
) -> list[Self]:
"""同步按统一媒体身份查询候选订阅列表。"""
condition = cls._identity_condition(
media_source=media_source,
@@ -318,7 +324,7 @@ class Subscribe(Base):
media_source: MediaSource | str | None = None,
media_id: str | None = None,
music_type: Optional[str] = None,
):
) -> list[Self]:
"""异步按统一媒体身份查询候选订阅列表。"""
condition = cls._identity_condition(
media_source=media_source,
@@ -339,7 +345,7 @@ class Subscribe(Base):
media_id: str | None = None,
season: Optional[int] = None,
music_type: Optional[str] = None,
):
) -> Optional[Self]:
"""
根据条件查询订阅
"""
@@ -349,7 +355,8 @@ class Subscribe(Base):
statement = select(cls).where(condition, cls.type == type)
if season is not None:
statement = statement.where(cls.season == season)
return db.execute(statement).scalars().first()
subscription: Optional[Self] = db.execute(statement).scalars().first()
return subscription
@classmethod
async def async_get_by(
@@ -360,7 +367,7 @@ class Subscribe(Base):
media_id: str | None = None,
season: Optional[int] = None,
music_type: Optional[str] = None,
):
) -> Optional[Self]:
"""
根据条件查询订阅
"""
@@ -371,10 +378,11 @@ class Subscribe(Base):
if season is not None:
query = query.filter(cls.season == season)
result = await db.execute(query)
return result.scalars().first()
subscription: Optional[Self] = result.scalars().first()
return subscription
@classmethod
def list_by_username(cls, db: Session, username: str, state: Optional[str] = None, mtype: Optional[str] = None):
def list_by_username(cls, db: Session, username: str, state: Optional[str] = None, mtype: Optional[str] = None) -> list[Self]:
"""在调用方 Session 中按用户筛选订阅。"""
statement = select(cls).where(cls.username == username)
if state:
@@ -386,7 +394,7 @@ class Subscribe(Base):
@classmethod
async def async_list_by_username(
cls, db: AsyncSession, username: str, state: Optional[str] = None, mtype: Optional[str] = None
):
) -> list[Self]:
"""在调用方 AsyncSession 中按用户筛选订阅。"""
statement = select(cls).where(cls.username == username)
if state:
@@ -397,7 +405,7 @@ class Subscribe(Base):
return list(result.scalars().all())
@classmethod
def list_by_type(cls, db: Session, mtype: str, days: int = 7):
def list_by_type(cls, db: Session, mtype: str, days: int = 7) -> list[Self]:
"""在调用方 Session 中按类型查询最近时间窗内的订阅。"""
return list(
db.execute(
@@ -411,7 +419,7 @@ class Subscribe(Base):
)
@classmethod
async def async_list_by_type(cls, db: AsyncSession, mtype: str, days: int = 7):
async def async_list_by_type(cls, db: AsyncSession, mtype: str, days: int = 7) -> list[Self]:
"""在调用方 AsyncSession 中按类型查询最近时间窗内的订阅。"""
result = await db.execute(
select(cls).where(

View File

@@ -75,7 +75,7 @@ uv run --locked --no-sync python scripts/architecture/complexity.py
回调和返回值类型;全仓错误计数为零并不保证单独纳入 strict frontier 后也为零。
2026-10-16 开始逐文件清理类型债务,`app/domain/title.py` 通过局部可空类型声明保留
既有搜索解析行为,并纳入严格清单;当前清单共 134 个文件(133 个生产文件和 1 个脚本)。
既有搜索解析行为,并纳入严格清单;当前清单共 135 个文件(134 个生产文件和 1 个脚本)。
每个治理提交聚焦一个生产文件及其必要测试、门禁配置、真实下降基线和指标文档。
验收同时检查完整严格清单与全宿主 Linux 分析,避免 `follow_imports = skip` 隐藏依赖
签名冲突;不通过忽略诊断或放宽类型合同接受回退。最新文件清单以 `mypy.ini` 为准,

View File

@@ -106,9 +106,9 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
| Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 |
| Module Contract | 215 specs / 211 methods / 263 calls | 动态方法名为 0;分类事实富化通过显式合同进入插件调度,旧 YAML 分类方法已移除;download 新增 custom_words 可选参数,旧 provider 签名通过显式合同兼容 |
| Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement |
| Python 源码量 | 372,650 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
| Python 源码量 | 372,658 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
| 长方法 | 309 个超过 80 行 | 本次 AST 扫描排除 `app/plugins/**`;61 个超过 150 行,17 个超过 250 行;新增函数仍受 C901/嵌套深度门禁约束 |
| 全量 mypy 历史债务 | 8,601 / 422 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
| 全量 mypy 历史债务 | 8,575 / 421 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
| Ruff 历史诊断 | 98 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
| 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 |

View File

@@ -120,6 +120,7 @@ files =
app/db/models/siteicon.py,
app/db/models/sitestatistic.py,
app/db/models/siteuserdata.py,
app/db/models/subscribe.py,
app/db/models/subscribehistory.py,
app/db/models/systemconfig.py,
app/db/models/transferhistory.py,

View File

@@ -977,10 +977,6 @@
"no-any-return": 1,
"redundant-cast": 2
},
"app/db/models/subscribe.py": {
"arg-type": 8,
"no-untyped-def": 18
},
"app/db/oper/downloadhistory.py": {
"no-any-return": 1,
"no-untyped-def": 9,