grantjenks / blue

The slightly less uncompromising Python code formatter.
https://blue.readthedocs.io/
Other
393 stars 21 forks source link

Continuation lines defined by extra indentation #89

Open flexatone opened 1 year ago

flexatone commented 1 year ago

Thank you for starting this project!

For a very long time I have been using an extra indent to distinguish continuation lines. This is defined as an option in PEP 8:

# Correct:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

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

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

I use this consistently for function definitions or any other line continuation. The rule is simple, and I find it significantly helps in distinguishing line continuations from blocks of code created by loops, branches, function/class definitions, etc. This is a silly example but it conveys the idea:


def foo(
        a: int, # argument indented twice to distinguish from body of function
        b: float,
        ):
    c = a + b
    if (c >  20 
            or a < 0  # line continuation indented to distinguish body of `if`
            or b > 3): 
        c *= 2 
    d = bar(a=a,  
            b=b, # line continuation from function call
            c=c,
            )
    return d

Real code examples can be seen here:

https://github.com/static-frame/static-frame/blob/master/static_frame/core/series.py

I have not used black because it eliminates the information provided by these extra indents. I would be thrilled if blue might support this. I could work on a PR if there is interest.

jsh9 commented 1 year ago

This particular style choice of Black also troubles me. That's what motivated me to start a more configurable formatter based on a fork from Black: https://github.com/jsh9/cercis

My project already supports indenting by 8 spaces at function definitions. And I plan to add more configurable options.

Please feel free to try it out and provide feedback.