Pylint raise-missing-from [英] Pylint raise-missing-from

查看:176
本文介绍了Pylint raise-missing-from的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pylint message (w0707) on this piece of code (from https://www.django-rest-framework.org/tutorial/3-class-based-views/):

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

消息是:
考虑使用 'from' 关键字显式重新引发
我不太明白如何采取行动来纠正这个问题.
预先感谢您的帮助

the message is:
Consider explicitly re-raising using the 'from' keyword
I don't quite understand how to act to correct the problem.
Thank you in advance for your help

推荐答案

上述问题评论中的链接概述了问题并提供了解决方案,但为了让像我这样直接登陆此页面的人清楚,无需转到另一个线程,阅读并获取上下文,这里是您特定问题的答案:

这可以通过为您排除"的异常取别名并在您的第二次加注中引用它来解决.

使用上面的代码片段,请参阅底部两行,我添加了under-carets"来表示我添加的内容.

Taking your code snippet above, see the bottom two lines, I've added 'under-carets' to denote what I've added.

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
#                         ^^^^^^^^^^^^^^^^^^

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

这篇关于Pylint raise-missing-from的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆