Pylint 引发缺失

Pylint raise-missing-from

关于这段代码(来自https://www.django-rest-framework.org/tutorial/3-class-based-views/)我有一条pylint消息(w0707):

class SnippetDetail(APIView):
    """
    Retrieve, update or delete a snippet instance.
    """
    def get_object(self, pk):
        try:
            return Snippet.objects.get(pk=pk)
        except Snippet.DoesNotExist:
            raise Http404

留言是:

Consider explicitly re-raising using the 'from' keyword

我不太明白如何解决问题。

上面对你问题的评论中的 link 概述了问题并提供了解决方案,但为了清楚起见,像我一样直接登陆此页面的人,而不必去另一个线程,阅读并获取上下文,这里是您的特定问题的答案:

TL;DR;

这可以简单地通过为异常添加别名 'excepting' 并在第二次加注中引用它来解决。

使用上面的代码片段,查看底部两行,我添加了 'under-carets' 来表示我添加的内容。

class SnippetDetail(APIView):
    """
    Retrieve, update or delete a snippet instance.
    """
    def get_object(self, pk):
        try:
            return Snippet.objects.get(pk=pk)
        except Snippet.DoesNotExist as snip_no_exist:
#                                   ^^^^^^^^^^^^^^^^
            raise Http404 from snip_no_exist
#                         ^^^^^^^^^^^^^^^^^^

注意:别名可以是任何格式正确的字符串。