Flake8 抛出 B008 fastapi 数据类型定义

Daw*_*y33 3 python typing flake8 fastapi

我有以下代码,它POST通过 处理请求fastapi。但是,flake8不断抛出B008 Do not perform function calls in argument defaults. The call is performed only once at function definition time错误。

@app.post("/predict", status_code=200)
def predict(
    page_no: int = Form(...), dimensions: list = Form(...), image: UploadFile = File(...)
) -> Dict[str, int]:
Run Code Online (Sandbox Code Playgroud)

有没有办法修复或解决这些警告?

Eli*_*les 6

一种解决方案是更新 flake8 配置(例如在文件中setup.cfg)以忽略此规则,Depends(...)如下所示:

extend-immutable-calls = Depends, fastapi.Depends, fastapi.params.Depends
Run Code Online (Sandbox Code Playgroud)

截至 2022 年 12 月,您必须手动列出名称,因为 flake8-bugbear 插件不知道实际类型(它是 AST 解析器),因此存在一个未解决的问题

查看更多: