python - Pylint raise-missing-from

标签 python pylint

我在这段代码(来自 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我不太明白如何采取行动来纠正这个问题。
预先感谢您的帮助

最佳答案

上面对您的问题的评论中的链接概述了问题并提供了解决方案,但为了清楚那些像我一样直接登陆此页面的人,而不必转到另一个线程,阅读并获取上下文,这里是您的答案具体问题:
TL; 博士;
这可以通过为您“排除”的 Exception 取别名并在您的第二次加注中引用它来简单地解决。
使用上面的代码片段,请参阅底部两行,我添加了“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
#                         ^^^^^^^^^^^^^^^^^^
注意:别名可以是任何格式正确的字符串。

关于python - Pylint raise-missing-from,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63738900/

相关文章:

python - 错误 : No matching distribution found for pandas - "pip install pandas"

macos - 安装 pylint OSX 忽略可执行文件

pycharm - 如何使用 pyproject.toml 为 PyCharm 配置 pylint?

Python导入模块导入另一个模块

Python Web 使用过滤器抓取表格

python - 在 Ipython 中使用 Pylint(Jupyter-Notebook)

pylint3 变量名称 "fl"不符合 Snake_case 命名风格(名称无效)

python - 在 Windows 中设置默认的 pylint config.rc 文件

python - 为什么这个循环只有一次迭代,而不是用户输入的迭代次数?

python - Keras:简单数据的简单神经网络不起作用