Automatic formatting of Python code

Josh Warner silvertrumpet999 at gmail.com
Thu Apr 2 22:34:37 EDT 2015



yapf works fairly well. It still needs some work to be truly PEP8 
compliant, especially regarding math expressions, and this is an issue I 
have with most PEP8 style checkers.

This is the actual snippet provided by PEP8 as an example of what not to do:

i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)

which, in a true PEP8 style checker/formatter, should change to

i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)

again, this isn't a contrived example, it's directly from PEP8 
<https://www.python.org/dev/peps/pep-0008/>! Yet all style checkers I know 
of get the first two lines right, then fail on the last three thinking the 
bad snippet is correct while throwing a fit over the correct (and much more 
readable) one. yapf does the same.

Unfortunately the only real way to correct this in the community is to fix 
the style checkers. So long as our automated tools throw a fit over the 
ideal formatting, this behavior is going to get more entrenched. There is a 
lot of inertia to overcome, but it might be worth bucking the trend. It 
does essentially require us to teach the checker order of operations.

Another part of PEP8 which almost nobody obeys for similar reasons pertains 
to complicated slicing operations. The following is considered correct, and 
I know violations of these are littered all over the package.

ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

Food for thought!
Josh

On Wednesday, April 1, 2015 at 12:56:27 AM UTC-5, stefanv wrote:

On Tue, Mar 31, 2015 at 10:18 PM, Juan Nunez-Iglesias wrote: 
> > From Google, a tool to automatically format your Python code, even 
> beyond 
> > what PEP8 prescribes: 
> > 
> > https://github.com/google/yapf 
> > 
> > I always thought Go's gofmt tool (and convention) were a great asset to 
> that 
> > community. It'd be awesome to have the same for Python. 
>
> I think you should run the scikit-image code through YAPF, make pull 
> requests accordingly, and claim the glory for yourself. 
>
> I'd be happy to review :) 
>
> Stéfan 
>
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20150402/5d07aa8a/attachment.html>


More information about the scikit-image mailing list