Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

E126 hanging indent is too strict #167

Closed
methane opened this Issue · 17 comments

6 participants

INADA Naoki Florent Xicluna Ian Cordasco Steven Myint Colton Myers Chris Lasher
INADA Naoki
from os import(
        path,
        listdir)

This cause "E126 continuation line over-indented for hanging indent".
But below code is OK. from ... import ( should be handled like that.

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

Thank you for your feedback.

AFAICT, the first example does not trigger any warning with the pep8 tool.

For the second example, this was a deliberate choice of the E12 checks on issue #64 (@samv proposal). The PEP-8 itself advocates for "Use 4 spaces per indentation level." without excluding continuation lines indentation from this rule.
The spirit of the E12 check is to enforce PEP-8 for continuation lines, and to avoid inconsistent indentations through the code.

Of course each check can be ignored with --ignore E12 for example.

INADA Naoki

I'm sorry about wrong first example.
What I want to say is:

from os import(
        path,
        listdir,
        )

This cause "E126 continuation line over-indented for hanging indent" and
"E123 closing bracket does not match indentation of opening bracket's line".

Ian Cordasco

@methane an alternate way of writing that is:

from os import (path, foo, bar, bogus, #etc
                listdir, unlink, #etc,
                )
Steven Myint

@methane, you have one too many indents.

from os import (
    path,
    listdir,
)
INADA Naoki

@myint That rule isn't applied to hanging indent.

Florent Xicluna
Collaborator

Well, the PEP 8 recommends "Use 4 spaces per indentation level."
And there's no objective reason to apply a different rule for continuation line indentation.

The purpose of these E12* rules is to conform to PEP 8 and enforce some consistency for the indentation.

INADA Naoki

"per level" means indent for block.
It's not for continuation line.

Florent Xicluna
Collaborator

"per level" means indent for block.
It's not for continuation line.

This is your interpretation of PEP 8.
If you want to be creative about your indentation, you can disable the E12 checks in the configuration.

INADA Naoki

This is your interpretation of PEP 8.

pep8 says:

there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.
...
Optional:

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

http://www.python.org/dev/peps/pep-0008/#indentation

I believe no one can interpret this to "hanging indent should be 1-level 4-space indent."

If you want to be creative about your indentation, you can disable the E12 checks in the configuration.

Yes, I already disabled it. But I think too strict default hurts new users.

Colton Myers

I have a similar "too strict" issue with E126.

cmd = 'git archive{prefix}{fmt} -o {output} {rev}'.format(
        rev = rev,
        output = output,
        fmt = ' --format={0}'.format(fmt) if fmt else '',
        prefix = ' --prefix="{0}"'.format(prefix if prefix else basename)
)

triggers E126, but I think this should be allowed to distinguish from an actual new indent level. Pulling the last ) onto the line above makes no difference.

Steven Myint

@basepi, --ignore=E126. (The whole point of E126 is to complain about indentation greater than 4 spaces.)

Colton Myers

@myint Heh, thanks for this. I was just wondering if we wanted to try to bring it in line with PEP8. =)

Steven Myint

I think 4 is the most used convention in the Python code base. The following will show lines that end with ( and the line that immediately follows it.

locate .py | grep '\.py$' | grep python3.3 | xargs grep -A1 '($'

So in terms of following a convention, I think E126 is the way to go. If one prefers 8 instead 4, then I think --ignore=E126 is probably the best option.

Colton Myers

Cool! Thanks for the quick reply.

INADA Naoki

@myint Thank you. I agree E126 is not too strict for now.

INADA Naoki methane closed this
Chris Lasher

@methane said

I believe no one can interpret this to "hanging indent should be 1-level 4-space indent."

The 4aec4d180429 revision of PEP-8, updated 2014-05-20, explicitly aligns with @methane's assertion:

The 4-space rule is optional for continuation lines.

Optional:

# Hanging indents *may* be indented to other than 4 spaces.
foo = long_function_name(
  var_one, var_two,
  var_three, var_four)

pep8 needs to revise its hanging indent rules. The number of spaces within a hanging indent needs only to be consistent; 2 spaces, 4 spaces, 8 spaces – all are valid amounts of space for hanging indents.

Ian Cordasco

@gotgenes I disagree that pep8 needs a fix because in the Yes section (which you helpfully omitted) it says:

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

And the example level it uses is 4 spaces. In other words, pep8 is still correct, and the ability to turn off this check keeps with the PEP's understanding of optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.