我正在将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)
结果:
$ 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识别,我怀疑在mypy评估期间未添加元类BasePermissionMetaclass(包含操作)。我认为只需安装djangorestframework-stubs并在mypy.ini中对其进行配置就足够了。