سؤال

I have this statement as a few lines:

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

As it stands, using the PEP8 script, it gives me an "E128: continuation line under-indented for visual indent" error on the second line.

I've tried a whole bunch of different ways of formatting, and the only way I can get PEP8 to stop complaining is:

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

But this looks like garbage.

Suggestions? E124, E126, and E128 seem to be a huge pain!

I don't mind solutions which have the { on the first line (or on it's own), but I hope there's a solution where the }, and context_instance... are at the same indentation level.

هل كانت مفيدة؟

المحلول

The problem is that all parameters are supposed to be indented to the same level. That includes any parameter(s) on the initial function call line.

So, while you could fix it like this:

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

… that'll usually just make you run afoul of the 80-column rule, and will certainly make your code uglier even if pep8 doesn't complain. What you probably want is this:

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

Or, of course, you could just break up your giant expression:

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

نصائح أخرى

Have you ever tried with django-annoying ?

you can do this...

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

I think this is cleaner and it can help you with the PEP8 style...

I'm pretty sure it wants you to indent everything to the opening parens (if you need a parameter up there) -- i.e.

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

otherwise,

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

should also be legal.

Or some such. See the pep docs on proper indentation practices

Here are the relevant examples from the specification, for a passing wanderer:

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top