Python:Pep8 E128 缩进错误...如何修复?

20

我有这样一句话,分成几行:

    return render_to_response('foo/page.html',
        {
            'situations': situations,
            'active': active_req,
        },
        context_instance=RequestContext(request))

目前使用PEP8脚本时,在第二行会出现“E128: continuation line under-indented for visual indent”错误。

我尝试了很多不同的格式方式,唯一让PEP8停止抱怨的方式是:

    return render_to_response('foo/page.html', {
        'situations': situations,
        'active': active_req,
    },
        context_instance=RequestContext(request))

但这看起来像垃圾。

有什么建议吗? E124、E126和E128似乎非常麻烦!

我不介意第一行(或独立的一行)有{的解决方案,但我希望有一个解决方案,其中},context_instance... 处于同一缩进级别。

但这看起来像垃圾。

有什么建议吗?E124、E126和E128似乎非常麻烦!

我不介意第一行(或独立的一行)有{的解决方案,但我希望有一个解决方案,其中},context_instance...处于同一缩进级别。


你需要这个所有的语句都是一个带有一个巨大表达式的原因吗?将其拆分不会使其更长,这意味着您甚至不必担心任何复杂的样式规则,并且它可能更易读和更易于调试。 - abarnert
3个回答

23

问题在于所有参数都应该缩进到同一级别。这包括初始函数调用行上的任何参数。

因此,虽然您可以像这样修复它:

return render_to_response('foo/page.html',
                          {
                              'situations': situations,
                              'active': active_req,
                          },
                          context_instance=RequestContext(request))

…那通常只会让你违反80列规则,并且即使pep8没有抱怨,也肯定会让你的代码更难看。你可能想要的是这个:

return render_to_response(
    'foo/page.html',
    {
        'situations': situations,
        'active': active_req,
    },
    context_instance=RequestContext(request))

当然,你也可以将庞大的表达式拆分:

d = {
    'situations': situations,
    'active': active_req,
}
context = RequestContext(request)
return render_to_response('foo/page.html', d, context_instance=context)

感谢您详尽的回答和解释。非常清晰易懂。 - Joseph

4

我相信它希望您将所有东西缩进到开括号(如果您需要一个参数) - 也就是说,

return render_to_response('foo/page.html',
                          {
                              'situations': situations,
                              'active': active_req,
                          },
                          context_instance=RequestContext(request))

否则,
return render_to_response(
    'foo/page.html',
    {
        'situations': situations,
        'active': active_req,
    },
    context_instance=RequestContext(request))

还应该是合法的。

或者类似的情况。请参阅有关正确缩进惯例的PEP文档

以下是规范中相关的示例,供路过此处的人参考:

Yes:

# Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)
No:

# Arguments on first line forbidden when not using vertical alignment
foo = long_function_name(var_one, var_two,
    var_three, var_four)

# Further indentation required as indentation is not distinguishable
def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)
Optional:

# Extra indentation is not necessary.
foo = long_function_name(
  var_one, var_two,
  var_three, var_four)

它不希望您将所有内容缩进到开放括号中。如果是这样的话,第二个就不会起作用。它只是希望您将所有参数缩进到相同的级别,就像我在我的答案中解释的那样。 - abarnert
@abarnert 我的意思是在他特定的情况下——他的第一个参数位于“左括号”处。如果他想要在那里放置一个参数,它确实需要与起始分隔符对齐。根据 PEP 8 的规定: - vroomfondel
续行应该使用垂直对齐的方式来对齐Python中括号、方括号和大括号内的换行元素,或者使用悬挂缩进。当使用悬挂缩进时,应考虑以下几点:第一行不应有参数,并且应使用进一步的缩进来清晰地区分其作为续行的身份。 - vroomfondel
好的,有了这个额外的澄清,没问题了。最初的表述方式确实有点令人困惑。PEP中的例子肯定很有帮助,特别是第二个和最后一个恰好涵盖了OP的情况。 - abarnert
@abarnert 没错,你说得对,我修复了它,因为你让我意识到它不够清晰。谢谢! - vroomfondel

4

你是否尝试过使用django-annoying

你可以这样做...

@render_to('foo/page.html')
def bar(request):
    return {'situations': situations,
            'active': active_req,}

我认为这样更加简洁,可以帮助你符合PEP8风格...


我还没有尝试过,但那看起来很棒。谢谢你的提示! - Joseph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接