autopep8 icon indicating copy to clipboard operation
autopep8 copied to clipboard

Closing brackets on new lines

Open benjaoming opened this issue 10 years ago • 12 comments

Thanks for a great script! I'm using it with a PyDev shortcut and it's extremely useful! I tried adapting it to run directly inside PyDev's Jython environment, but unfortunately it is Jython 2.2 and after rewriting a hundred lines of code and seeing that essential external libraries were missing, I gave up.

Anyways, here's a script that calls autopep8 though os.system to format the code directly inside the Eclipse editor. It's really so damn useful :dancers:

https://gist.github.com/benjaoming/9911641

But now for the issue... it's about line optional breaks before closing brackets, as the examples show here:

http://legacy.python.org/dev/peps/pep-0008/#id11

...there are a couple of acceptable ways to do indentation inside brackets. I personally prefer this style:

my_list = [
    1, 2, 3,
    4, 5, 6,
]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
)

It's appreciated that autopep8 will not touch the above formatting (because it's already valid). However, it would be nice if there was an option. For instance.

Input

result = some_function_that_takes_arguments('arg1', 'arg2', 'arg3', 'arg4', 'arg5', 'arg6', 'arg7', 'arg8')

Output

result = some_function_that_takes_arguments(
    'arg1',
    'arg2',
    'arg3',
    'arg4',
    'arg5',
    'arg6',
    'arg7',
    'arg8')

My idiosyncratic preferred output

result = some_function_that_takes_arguments(
    'arg1',
    'arg2',
    'arg3',
    'arg4',
    'arg5',
    'arg6',
    'arg7',
    'arg8',
)

Notice: The added line break before the bracket, and the added comma. I like this pattern, because I write django and there are often chained calls with QuerySets that become quite long.

Here is a more complex example:

models.Membership.objects.filter(account__in=owner, active=True, created__lte='2014-01-01').select_related('user').distinct().order_by('id')

...which using autopep8 --aggressive becomes:

models.Membership.objects.filter(
    account__in=owner,
    active=True,
    created__lte='2014-01-01').select_related('user').distinct().order_by('id')

But I would much prefer:

models.Membership.objects.filter(
    account__in=owner,
    active=True,
    created__lte='2014-01-01',
).select_related('user').distinct().order_by('id')

benjaoming avatar Apr 01 '14 10:04 benjaoming

Both styles look good to me. I'll accept a pull request to add such an option.

By the way, some time ago, I used to regularly test autopep8 against Jython 2.7 (I think). I didn't even know there was a 2.2 until now. :)

myint avatar Apr 01 '14 13:04 myint

It's shipped with PyDev.. at least I think it's Jython 2.2 because the CLI prompt says so. I have no idea how to make PyDev execute Jython with the external environment (2.5.2 is provided in Ubuntu 13.10). I tried replacing PyDev's Jython but that broke PyDev.

Thanks. I'll give it a go.. it's just a little hard to quickly understand what's going on in this rather long script :)

benjaoming avatar Apr 01 '14 23:04 benjaoming

As an overview, what autopep8 does, when reformatting long lines, is generate various reformatted line candidates. It then ranks them and selects the best candidate. @gwelymernans is working on an experimental line formatter that produces better looking candidates that are thrown into the mix (when --experimental is enabled).

myint avatar Apr 02 '14 01:04 myint

I hope this get added. (for function calls, lists, sets, ..) I'd like this as well. (The comma after the last item also makes it easy to add new elements.)

CreamyCookie avatar Jul 16 '14 17:07 CreamyCookie

Also hoping for this (and I can't figure out how to get notifications for this w/o adding a comment, it seems).

ncolton avatar Aug 07 '14 16:08 ncolton

@ncolton Press Ctrl + F and search for "Notifications" and click the "Subscribe"-button.

CreamyCookie avatar Aug 08 '14 19:08 CreamyCookie

Please implement this, then I really love autopep8.

spaceone avatar Oct 25 '16 12:10 spaceone

Is this implemented?

marius92mc avatar Oct 16 '17 13:10 marius92mc

Has this been added?

imdadahad avatar Feb 22 '18 01:02 imdadahad

Confirming that the original issue is still relevant as of the following version:

$ autopep8 --version
autopep8 1.4.3 (pycodestyle: 2.4.0)

golubitsky avatar Dec 27 '18 23:12 golubitsky

Is this an option now?

technolingo avatar Nov 20 '19 10:11 technolingo

If you are okay with using an external tool, Black handles this feature.

benjaoming avatar Nov 20 '19 15:11 benjaoming