mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-15 05:19:17 +00:00
chore(backend): 新增 basedpyright 校准配置,并修复它暴露的三处真缺陷
背景:仓库此前没有 pyright/basedpyright 配置,basedpyright 使用自身默认(≈recommended),
在 app/ 下产生 247 errors / 3799 warnings,其中 143 条是 reportMissingTypeArgument
(未参数化泛型)这类注解完整度噪音,会把真缺陷淹没(例如 OSS 的 Client.uploader 方法误用)。
配置(backend/pyrightconfig.json)
- typeCheckingMode: basic;显式把能抓真缺陷的规则保持为 error
(reportAttributeAccessIssue / reportArgumentType / reportOptionalMemberAccess /
reportIncompatibleMethodOverride / reportConstantRedefinition / reportMissingImports 等)
- 关闭注解完整度与风格类规则;tests/ 用 executionEnvironments 单独放宽(extraPaths 保证 app 可解析)
- 结果:app + main.py + tests 全量 0 errors
- 红绿验证:合成「str | None 传参」「MethodType 上取属性」「Optional 成员访问」三类
仍全部报 error,证明校准没有把真缺陷一起关掉
修复配置暴露的三处真缺陷
1) template_safety.py:_SQLALCHEMY_TYPE_WHITELIST 对全大写常量二次赋值 → 合并为一次成型
2) template_safety.py:normalize_column_type 假定 _TYPE_BASE_RE 必命中(用 # type: ignore 盖住,
None 时 .group() 会崩)→ 改为 None 安全,去掉 type-ignore
3) node/service.py ×2:CustomException(msg=reason) 中 reason 为 str | None,客户端可能收到空文案
→ 新增 SchedulerUtil.require_schedulable() 收敛「判定 + 抛异常 + 兜底文案」,调用点改为一行
验证:basedpyright 0 errors(原 247);ruff All checks passed;pytest 97 passed;
已部署并线上复验(四端 200、Host 收窄、伪造令牌被拒、验证码过期 400 保留业务文案、
容器内 require_schedulable 与 OSS 调用修正均已生效)
This commit is contained in:
@@ -327,9 +327,7 @@ def _add_job_with_trigger(
|
||||
raise ValueError("任务代码块不能为空")
|
||||
# 可调度性判定(单一事实来源):builtin: 引用走白名单、不经 exec,因此**开关关闭时也允许注册**;
|
||||
# 只有"原始代码块"才会被 SCHEDULER_ALLOW_CODE_EXEC=False 拦住,并在提示里给出 builtin 替代写法。
|
||||
schedulable, reason = SchedulerUtil.describe_schedulability(code_block)
|
||||
if not schedulable:
|
||||
raise CustomException(msg=reason)
|
||||
SchedulerUtil.require_schedulable(code_block)
|
||||
|
||||
jobstore = job_info.jobstore or "default"
|
||||
executor = job_info.executor or "threadpool"
|
||||
@@ -416,9 +414,7 @@ def run_node_once(node: NodeModel) -> str:
|
||||
if not node.func or not node.func.strip():
|
||||
raise ValueError("任务代码块不能为空")
|
||||
# 手动执行同样按可调度性判定:builtin 引用可直接执行;原始代码块在开关关闭时给出替代写法
|
||||
schedulable, reason = SchedulerUtil.describe_schedulability(node.func)
|
||||
if not schedulable:
|
||||
raise CustomException(msg=reason)
|
||||
SchedulerUtil.require_schedulable(node.func)
|
||||
temp_job_id = f"{node.id}{_MANUAL_JOB_PREFIX}{datetime.now():%Y%m%d%H%M%S}"
|
||||
trigger = DateTrigger(run_date=datetime.now() + timedelta(seconds=0.1), timezone="Asia/Shanghai")
|
||||
_add_job_with_trigger(node, trigger, job_id_override=temp_job_id, name_suffix=" - 手动执行")
|
||||
|
||||
Reference in New Issue
Block a user