Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make pylint and autopep8 agree on how to indent wrapped function definitions?

How can I make the prettifier autopep8 and the linter pylint agree on how to indent my code, without disabling indentation formatting/linting altogether? I don't mind that much if it's the first or the second formatting, as long as I can hit Alt+F in VSCode and trust the output.

The prettifier wants the code to be indented like this...

# autopep8 prettifier
def sum(
    a: int,
    b: int
) -> int:
    """Return the sum of a and b."""
    return a + b

... but the linter wants it like this.

# pylint linter
def sum(
        a: int,
        b: int
    ) -> int:
    """Return the sum of a and b."""
    return a + b

The PEP8 standard lists this as a way to format the function, but they don't mention how to indent when the closing parenthesis is put on a separate line. I really prefer have the extra line break because this puts the output format on its own line, it reduces the urge to have a blank line as the first line in the body of the function. I slightly prefer the first option above as this one aligns the closing parenthesis with the line that has to opening one. Google’s Python Style Guide recommends the first indentation example when using type hints.

# PEP8 standard
def sum(
        a: int,
        b: int) -> int:
    """Return the sum of a and b."""
    return a + b

(For the above example, it would be better to simply put the function declaration on a single line, but this is not always possible without making the line too long.)

like image 268
Jan Aagaard Avatar asked Nov 17 '18 15:11

Jan Aagaard


1 Answers

You can upgrade to the latest pylint. bad-continuation and bad-whitespace were removed from pylint in version 2.6.0. The pylint team thinks that black or autopep8 can help you with formatting better than Pylint.

More generally, I think that if you're using an auto-formatter, then you can disable pylint's messages about formatting.

like image 120
Pierre.Sassoulas Avatar answered Nov 04 '22 21:11

Pierre.Sassoulas