“更新”没有重载与提供的参数匹配

问题描述 投票:0回答:1

我目前正在阅读 FastAPI 的教程用户指南,pylance 抛出以下警告:

“更新”没有重载与提供的参数匹配PylancereportCallIssue
Typing.pyi(690, 9):重载 2 是最接近的匹配

这里是抛出警告的代码:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q}) # warning here
    return results

我尝试将

Python › Analysis: Type Checking Mode
更改为
basic
并使用 Pylance 的预发布版本,但警告仍然存在。

python types fastapi typing pylance
1个回答
0
投票

q
的类型不明确。

    # At this point, mypy knows that q is either `None`, or _any_ `str`
    if q:  # This fails to disambiguate the situation -- could be "" or None
        assert len(q) > 0
        # Mypy _knows_ for sure that if there was no stack trace
        # and we made it here, then we definitely have a `str`
        results.update({"q": q}) # warning here

最好的方法是

if q is not None:

© www.soinside.com 2019 - 2024. All rights reserved.