styleguide icon indicating copy to clipboard operation
styleguide copied to clipboard

Should python function argument definitions be indented 4 or 8 spaces?

Open dseomn opened this issue 5 years ago • 3 comments

YAPF recently changed from indenting like

def f(
        a: str,
        b: str,
) -> None:
    pass

to:

def f(
    a: str,
    b: str,
) -> None:
    pass

I thought the first way was correct, but looking at the style guide more, I'm not actually sure what the correct behavior is. The indentation section doesn't say anything about function arguments in a def statement. The type annotation line breaking section does have examples, but those follow Google's internal 2-space indentation style instead of the external 4-space style.

dseomn avatar Apr 25 '20 18:04 dseomn

2nd one is correct examples might be similar to these: https://google.github.io/styleguide/pyguide.html#32-line-length https://google.github.io/styleguide/pyguide.html#34-indentation

shendeguize avatar May 07 '20 04:05 shendeguize

Neither of those sections have examples with def statements, so I don't think they point towards either way of indenting a def statement. Note that the second way can be really hard to read if the closing parenthesis isn't on a new line, since it's hard to see where the args end and the body starts:

def f(
    a: str,
    b: str) -> str:
    c: str = 'foo'
    return f'{a}: {b} - {c}'

dseomn avatar May 08 '20 21:05 dseomn

Actually, I prefer using def like:

def f(a: Text,
      b: Text):  # Align with 'a' at 1st line.
    ...

shendeguize avatar May 28 '20 07:05 shendeguize