使用多个权限类时,mypy Django rest框架不支持左操作数类型

2024-05-23 18:40:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在现有的代码库上集成mypy(使用django、drf框架)。你知道吗

中的示例代码视图.py地址:

from rest_framework.permissions import IsAdminUser, IsAuthenticatedOrReadOnly

@api_view()
@permission_classes([IsAuthenticatedOrReadOnly | IsAdminUser])
def listAbc(request):
    queryset = ...
    serializer = ...
    return Response(serializer.data)

结果:

$ mypy
error: Unsupported left operand type for | ("Type[IsAuthenticatedOrReadOnly]")

使用的插件:

$ pip list | grep stubs
django-stubs                    1.2.0
djangorestframework-stubs       1.0.0

mypy配置文件(mypy.ini文件)地址:

[mypy]

plugins =
    mypy_django_plugin.main, mypy_drf_plugin.main

;ignore_missing_imports = True
files=**/*.py

[mypy-*.migrations.*]
ignore_errors = True

[mypy.plugins.django-stubs]
django_settings_module = project.settings

用mypy(0.720和0.740)检查。你知道吗

有什么问题吗? 由于mypy无法识别操作“|”,我怀疑元类BasePermissionMetaclass(包含这些操作)在mypy求值期间没有添加BasePermission。我假设只是安装djangorestframework存根并在中配置相同的存根mypy.ini文件够了。你知道吗


Tags: 文件django代码py地址pluginsplugindjangorestframework