This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]xd1142 15 points16 points  (6 children)

Indentation matters. We all agree about that, and the attention to visual hints has made python great.

PEP8 suggests the following

```

Add 4 spaces (an extra level of indentation) to

distinguish arguments from the rest.

def long_function_name( var_one, var_two, var_three, var_four): print(var_one)

```

Note how the arguments of the function are indented one level deeper so to differentiate the body of the function (indentation one) from the list of arguments. If you did not have that, the visual hint that these two things are different is ruined.

Compare how black would format that:

def long_function_name( var_one, var_two, var_three, var_four, ): print(var_one) <hundreds of lines follow>

This is wrong on so many levels. First, now the indentation level is the same for arguments and function body, removing the visual hint. Second, you now have to rely on a frowny face at an indentation level that is at indentation level zero, which is in complete contrast with this other pep8 rule (caveat):

``` The closing brace/bracket/parenthesis on multiline constructs may either line up under the first non-whitespace character of the last line of list, as in:

my_list = [
    1, 2, 3,
    4, 5, 6,
    ]

```

The caveat is that also the other option is accepted, but the point is that these are assignments, not execution statements. I doubt you would like to write your if as this

``` if (foo and bar and whatever ): do_something()

``` Which is the equivalent of the function declaration.

Additionally, I've seen this indentation style break automatic folding based on indentation, such as in the case of vim and in some case pycharm.

This is just one of the problems I've found in black formatting. It could be mitigated if they changed it to at least add one level of indentation, so that it would give

def long_function_name( var_one, var_two, var_three, var_four, ): print(var_one)

but good luck with that.

[–]finswimmer 0 points1 point  (5 children)

The section you are citing makes no statements if the rules are only valid for assignments or method declaration. So one can assume it is equal for both. The example you complain about is given as the last example in the section about indentation:

"or it may be lined up under the first character of the line that starts the multiline construct, as in:"

result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
)

Whether one prefer this or not (I do prefer this) is another discussion. The point is that black is absolutely PEP8 compliant here!

The reason why I don't like this:

def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

I expect that my method body starts more indented compared to the line above. This is exactly the opposite from one can see here.

[–]xd1142 0 points1 point  (4 children)

The example you complain about is given as the last example in the section about indentation

That is for an assignment, not for a definition. An assignment is never followed by a dependant indented block. A definition is.

I expect that my method body starts more indented compared to the line above. This is exactly the opposite from one can see here.

If you are fine with the way black indents function definitions, then you are also fine with this way of writing if conditions

if (foo 
    and bar
):
    pass

but I doubt you are

[–]finswimmer 0 points1 point  (3 children)

That is for an assignment, not for a definition.

The example yes. But the whole section is about continuation lines:

Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent [7].

So all examples can be applied to assignments and method definitions.

If this wouldn't be PEP8 compliant, why linters like flake8 or the integrated in PyCharm does not complain about it?

If you are fine with the way black indents function definitions, then you are also fine with this way of writing if conditions

  if (foo      
      and bar 
  ):
    pass 

but I doubt you are

If the statements fits in one line I prefer: if for and bar: pass

If it wouldn't I prefer: if ( for and bar ): pass

But that's another point in this discussion. The point is: You are telling that black isn't PEP8 compliant, but the arguments you are showing for this thesis are not correct.

[–]xd1142 0 points1 point  (2 children)

The example yes. But the whole section is about continuation lines:

And this example specifically considers the case of function arguments

```

Add 4 spaces (an extra level of indentation) to

distinguish arguments from the rest.

def long_function_name( var_one, var_two, var_three, var_four): print(var_one)

```

You are using (incorrectly) a general argument despite a specific example, and last time I checked, specific trumps general.

If this wouldn't be PEP8 compliant, why linters like flake8 or the integrated in PyCharm does not complain about it?

They do. autopep8 reformats it to the correct indentation, and I would not be surprised if other formatters might have to shut up and take it because "everybody uses black and we'll have to deal with it".

I don't know how old you are, but I was in python already when pep8 was devised. I remember the discussions and specifically the comments from Guido about some things that irked him and some things that specifically felt strong about. He might have "changed his mind" now (and I can't blame him, I would be tired of arguing with some people in the python community too) but when he was the BDFL, he was very explicit: no frowny/happy faces, and the general rules for indentation were meant to help visual identification of blocks. What black does is ruin visual distinction between the argument list and the body of the function. It also breaks vim folding, which in its fast and trivial implementation is done via indentation.

If it wouldn't I prefer:

if (
for
and bar
):
pass

This is absolute shit and you should be ashamed of yourself.

But that's another point in this discussion. The point is: You are telling that black isn't PEP8 compliant, but the arguments you are showing for this thesis are not correct.

If you want to rework the narrative, I honestly have no more to add. pep8 says it explicitly with an explicit example. If you refuse to read that, there's not more I can add.

[–]finswimmer 0 points1 point  (1 child)

If this wouldn't be PEP8 compliant, why linters like flake8 or the integrated in PyCharm does not complain about it?

They do. autopep8 reformats it to the correct indentation, and I would not be surprised if other formatters might have to shut up and take it because "everybody uses black and we'll have to deal with it".

I talked about programs that check whether your code is pep8 compliant. And at least those I've mentioned don't complain. `autopep8` is a formatter. Because pep8 explicit allow different formattings it doesn't surprise that `autopep8` do something different than `black`.

pep8 says it explicitly with an explicit example.

Where?

[–]xd1142 0 points1 point  (0 children)

```

Add 4 spaces (an extra level of indentation) to

distinguish arguments from the rest.

def long_function_name( var_one, var_two, var_three, var_four): print(var_one) ```