jcrocholl/pep8 — pull request #64 by samv (2012-05-27 07:13:43+00:00)
Title: Implementing continuation line indentation recommendations from PEP8

Hey there,

I have just done some work to write pep8 checks for continuation line parsing. I don't know what your normal procedure is for introducing new checks which may frustrate some people, but I think these are pretty important. It will, for instance, allow pep8.py to accept and reject the very first examples in the PEP8 document marked as "Yes" and "No". I kept every single error as its own code, though perhaps one or two of them could be folded into the same code.

Two thoughts occurred to me towards making it easier to add more checks to pep8.py: one would be to make the checkers "yield" each error they find, allowing a skipped error to not mask a desired error, and secondly some kind of general "make errors more recent than v1.1 warnings" or some such, to allow people who newly encounter the errors to have time to deal with them. I'd be happy to work on such features if it would help acceptance of these new checks.

---8<---
PEP8 has some specific recommendations early on which relate to
indenting continuation lines. Specifically, it distinguishes between
'visual indenting' and 'hanging indents'. This change implements these
rules, adding several new errors (for now):

* E120 - traps on a visual indent with hanging indent lines following
* E121 - traps overly–indented lines inside visual indent regions
* E122 - catches the evil and wrong indenting default that emacs'
python-mode applies to 'loose fingernails' (not canonical)
* E123 - catches continuation lines missing extra indentation when the
previous line started a hanging indent
* E124 - catches continuation lines not aligned on 4 spaces; note this
required a fix to the E23.py test
* E125 - catches continuation lines with 8 or more space indents
relative to the previous line without good reason
* E126 - ensures that indentation leaves blocks visually distinct

All of these errors are directly justifiable from text and examples in
the PEP8 document, and some examples are directly copied from PEP8. The
exception to this is E122, which the PEP8 standard appears neutral on
but nonetheless I feel is intrinsically related with the rules which are
defined here; to not enforce it would permit multiple valid indent
levels for closing brackets which start lines, leading to ambiguity.
All the PEP8 examples leave the closing bracket on the previous line,
avoiding this question. This E122 rule agrees with K&R, KNF, Linux,
this VIM auto–indent script:

http://www.vim.org/scripts/script.php?script_id=974

and the fix to python-mode achieved by the elisp fragment on:

http://stackoverflow.com/revisions/5361478/2

See testsuite/E12.py for many examples.