fastapi的依赖()原因是“不兼容的参数默认值”。 mypy错误

发布于 2025-01-21 07:24:43 字数 710 浏览 2 评论 0原文

我有这个示例代码。

def get_test(q: str) -> str:
    return 'str str str' + q


@router_user.put(
    "/", response_model=schemas.UpdateUserRequest, tags=["User and Authentication"]
)
async def update_user(
        data: schemas.UpdateUserRequest,
        q: str = Depends(logic.get_test),
        db: AsyncSession = Depends(get_db),
        user: models.User = Depends(logic.get_curr_user_by_token),
):
    print(q)

在用mypy检查时,我总是会遇到错误


app/api/user/router.py:74: error: Incompatible default for argument "q" (default has type "Depends", argument has type "str")  [assignment]

I have this example code.

def get_test(q: str) -> str:
    return 'str str str' + q


@router_user.put(
    "/", response_model=schemas.UpdateUserRequest, tags=["User and Authentication"]
)
async def update_user(
        data: schemas.UpdateUserRequest,
        q: str = Depends(logic.get_test),
        db: AsyncSession = Depends(get_db),
        user: models.User = Depends(logic.get_curr_user_by_token),
):
    print(q)

when checking it with mypy, I always get an error


app/api/user/router.py:74: error: Incompatible default for argument "q" (default has type "Depends", argument has type "str")  [assignment]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2025-01-28 07:24:43

看起来您使用了不正确的导入。

我已经清理了您的代码示例并添加了导入,并且以下确实通过了mypy

from fastapi import APIRouter, Depends


def get_test(q: str) -> str:
    return "str str str" + q


router_user = APIRouter()


@router_user.put("/")
async def update_user(
    q: str = Depends(get_test)
) -> None:
    print(q)

但是,如果我替换导入,

from fastapi import APIRouter
from fastapi.params import Depends

那么我会得到您从mypy遇到的相同错误。

我猜想您的IDE错误地从fastapi.params而不是直接从fastapi中完成导入。如果您检查官方 docs ,您会看到该>依赖性应直接从fastapi直接导入。

为什么发生这种情况

params.decide是定义的类在这里。但是,如果您直接使用它,则每当您编写x:str = deweds(...)之类的内容时,它将是类型错误,因为params.deppers.deppers.depperss.deppers class class与str不兼容。我们真正想要的是Mypy忽略依赖的类型(),并假定其与任何类型兼容。

这就是为什么 in fastapi实际上是围绕params.depends “ nofollow noreferrer”>定义为 返回返回 yy 。

It looks like you've used an incorrect import.

I've cleaned up your code sample and added imports, and the following does pass mypy:

from fastapi import APIRouter, Depends


def get_test(q: str) -> str:
    return "str str str" + q


router_user = APIRouter()


@router_user.put("/")
async def update_user(
    q: str = Depends(get_test)
) -> None:
    print(q)

However, if I replace the imports with

from fastapi import APIRouter
from fastapi.params import Depends

Then I get the same error you got from mypy.

I'm guessing your IDE incorrectly completed the import from fastapi.params instead of directly from fastapi. If you check the official docs, you'll see that Depends should be imported directly from fastapi.

Why this happens

params.Depends is a class defined here. However, if you use that directly, it will be a type error whenever you write something like x: str = Depends(...), because the params.Depends class is not compatible with str. What we really want is for mypy to ignore the type of Depends() and assume it's compatible with any type.

That's why the Depends in fastapi is actually a wrapper function around params.Depends defined as returning Any.

栀子花开つ 2025-01-28 07:24:43

查看您的代码看起来像您的get_curr_user_by_token返回models.user依赖本身不会改变结果的类型,因此您只需使用依赖内的事物返回的结果。

q: models.User = Depends(logic.get_curr_user_by_token),

Looking at your code it looks like your get_curr_user_by_token returns models.User. Depends itself doesn't change the type of result, so you just use the one that is returned by thing inside Depends.

q: models.User = Depends(logic.get_curr_user_by_token),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文