Official coding standards ease reading of other people’s code and (usually) kill useless debates. When I code in Python, I follow PEP8. Being a VIM user, Syntastic & flake8 are my tools for the job.

There is one thing in the PEP8 coding style which I don’t comply with: line lengths. The standard says: “Limit all lines to a maximum of 79 characters.” This is really to hard to follow. A typical situation which is easy to imagine is:

class MyClass:
    def my_function():
        if cond1:
            if cond2:
                call_this_func_with_long_name(argument1, argument2, argument3, argument4)

This looks made up but happens very often in practice. The standard specifies multiple ways to deal with such a scenario. Luckily it also says “For […] a team that can reach agreement on this issue, it is okay to increase the […] maximum length to 99 characters”. 25% more helps a lot!

However, there is one situation where it is really awkward to follow the style guide: Using the str.format() method with a lot of arguments within a “long” string. Consider:

# not okay according to PEP8
log.warn('The {} of {} failed. Please check {} or {}'.format(method_name, step_name, logfile_name, db_entry)

# fine but ugly
log.warn('The {} of {} failed. Please check {} or {}'.
         format(method_name, step_name, logfile_name, db_entry)

The second version gets nasty once the wrapped format itself exceeds the 79 (or 99) characters, especially when using keywords within the replacement field, i.e. 'The answer is {answer}'.format(answer=42). Furthermore, diff‘ing commits where only some string content changed may yield multiple changed lines instead of a single line change (which it is semantically). I have to say that I also spend (lost?) quite some time in the past making the format invocation comply with the standard. For theses reasons, I decided to deviate from the PEP8 maximum line length when formatting strings. Obviously, I want to have Vim/Syntastic/flake8 help me with this …

The code linter flake8 can be configured per project or globally. The latter is done in the file ~/.config/flake8. In order to increase the maximum line length, you simply add the following to the file:

max-line-length = 99

Once this option is set, flake8 (invoked within Vim by Syntastic) will not grump anymore for lines lengths up to 99 characters. I have found flake8-putty to be an easy way to disable the annoying E501: line too long error specifically for string formatting. The following two regexps tell flake8-putty to ignore E501 for every line which contains the literal .format( or every line ending with a single quote.

max-line-length = 99
putty-ignore =
    /\.format\(/ : E501
    /'$/ : E501

Today’s screens are wide enough to view two Python files side by side even if they both have line lengths of 99 characters so I believe it is okay to slightly exceed PEP8’s 79 characters. All very long lines “only” contain strings and it is usually not the stuff someone is interested in when reading code.

Caveats: It may cause problems when the editor autowraps long lines but I don’t do that. Any CI system (or even Git hooks) enforcing PEP8 needs to be configured properly, too.