autopep8
autopep8 copied to clipboard
Closing brackets on new lines
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')
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. :)
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 :)
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).
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.)
Also hoping for this (and I can't figure out how to get notifications for this w/o adding a comment, it seems).
@ncolton Press Ctrl + F and search for "Notifications" and click the "Subscribe"-button.
Please implement this, then I really love autopep8.
Is this implemented?
Has this been added?
Confirming that the original issue is still relevant as of the following version:
$ autopep8 --version
autopep8 1.4.3 (pycodestyle: 2.4.0)
Is this an option now?
If you are okay with using an external tool, Black handles this feature.