Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Pep8 E128 indentation error... how can this by styled?

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.

like image 448
Joseph Avatar asked Aug 28 '13 20:08

Joseph


1 Answers

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)
like image 173
abarnert Avatar answered Sep 30 '22 08:09

abarnert