我正在将 mypy 集成到现有的代码库上(使用 django、drf 框架)。
view.py 中的示例代码:
from rest_framework.permissions import IsAdminUser, IsAuthenticatedOrReadOnly
@api_view()
@permission_classes([IsAuthenticatedOrReadOnly | IsAdminUser])
def listAbc(request):
queryset = ...
serializer = ...
return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)
结果:
$ mypy
error: Unsupported left operand type for | ("Type[IsAuthenticatedOrReadOnly]")
Run Code Online (Sandbox Code Playgroud)
使用的插件:
$ pip list | grep stubs
django-stubs 1.2.0
djangorestframework-stubs 1.0.0
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
使用 mypy(0.720 和 0.740)进行检查。
这里可能有什么问题?由于操作'|' mypy 无法识别,我怀疑在 mypy 评估期间未添加元类 BasePermissionMetaclass (包含操作) BasePermission。我认为只需安装 djangorestframework-stubs …