Blog by Sumana Harihareswara, Changeset founder

01 Nov 2013, 11:06 a.m.

PEP 8 Compliance

Hi, reader. I wrote this in 2013 and it's now more than five years old. So it may be very out of date; the world, and I, have changed a lot since I wrote it! I'm keeping this up for historical archive purposes, but the me of today may 100% disagree with what I said then. I rarely edit posts after publishing them, but if I do, I usually leave a note in italics to mark the edit and the reason. If this post is particularly offensive or breaches someone's privacy, please contact me.

It's easier to read and contribute to code when it's stylistically consistent. This is a reason why we have PEP 8, the style guide for Python code. It says things like:

Avoid extraneous whitespace in the following situations:...
Immediately before the open parenthesis that starts the argument list of a function call:
Yes: spam(1)
No: spam (1)
Certainly most of the Python code I run across follows that convention. So I got confused when I read Bicho code that sometimes had extraneous whitespace between function name and arguments. Sometimes it did and sometimes it didn't. From a note by one of the maintainers I inferred that Bicho's developers want code to comply with PEP 8.

So I decided to look for those discrepancies, so I could fix them. You can use the pep8 module to find instances of PEP 8 noncompliance, and you can give it arguments to narrow down to just one issue. A command like

$ python pep8.py --select=E211 Bicho/

gave me the list of lines with extraneous whitespace before the open parenthesis. (I've edited out some path-related cruft.) I thought I'd write a regex to fix those lines, but Julia and Leah kindly talked me into seeking out a pre-existing tool first, and I found Pep8ify.

$ pep8ify -f whitespace_before_parameters Bicho/

gave me the proposed fixes as readymade diffs. To make Pep8ify do those fixes:

$ pep8ify -f whitespace_before_parameters Bicho/ -w Bicho/

So now I've filed an issue with a pull request. (I also used Pep8ify to clean up some whitespace inconsistencies around operators like "+" and "=" while I was at it.)

Thanks to Szymon Guz's blog post for pointing me in the right directions.