Skip to content

Black violates pep8 recommendation with long argument list #1178

@stefanoborini

Description

@stefanoborini

Currently, black reformats long routine names as follow

# in:

def very_important_function(template: str, *variables, file: os.PathLike, engine: str, header: bool = True, debug: bool = False):
    """Applies `variables` to the `template` and writes to `file`."""
    with open(file, 'w') as f:
        ...

# out:

def very_important_function(
    template: str,
    *variables,
    file: os.PathLike,
    engine: str,
    header: bool = True,
    debug: bool = False,
):
    """Applies `variables` to the `template` and writes to `file`."""
    with open(file, "w") as f:

Desired style

The current style done by black violates pep8 recommendation

# 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)

The logic and motivation behind pep8 formatting, and against black, is that one wants to keep the indentation of the function definition continuation at a different level compared to the logic block. Otherwise, it's harder to differentiate what's one and what's the other, despite the indented frowny face.

In fact, flake8 reports a pep8 violation for a case such as this

    if path.suffix in (
        '.js', '.json'
        ):
        proto = Protocol.json

x.py:20:5: E125 continuation line with same indent as next logical line

Black current formatting would be equivalent to this

    if path.suffix in (
        '.js', '.json'
    ):
        proto = Protocol.json

Which we can probably all agree is a bad idea.

Additional context from python-ideas mailing list

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions