UtilitiesPython2/pep8.py

Tue, 24 Sep 2013 18:32:18 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 24 Sep 2013 18:32:18 +0200
branch
5_3_x
changeset 2931
552d5934c6f6
parent 2801
8ccc38b80dc2
child 3163
9f50365a0870
permissions
-rw-r--r--

Fixed an error in the pep8 checkers.

842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 # pep8.py - Check Python source code formatting, according to PEP 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5 # Copyright (C) 2006 Johann C. Rocholl <johann@rocholl.net>
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 # Permission is hereby granted, free of charge, to any person
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 # obtaining a copy of this software and associated documentation files
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # (the "Software"), to deal in the Software without restriction,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 # including without limitation the rights to use, copy, modify, merge,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # publish, distribute, sublicense, and/or sell copies of the Software,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # and to permit persons to whom the Software is furnished to do so,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 # subject to the following conditions:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 # The above copyright notice and this permission notice shall be
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 # included in all copies or substantial portions of the Software.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 # SOFTWARE.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Check Python source code formatting, according to PEP 8:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 http://www.python.org/dev/peps/pep-0008/
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 For usage and a list of options, try this:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 $ python pep8.py -h
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 This program and its regression test suite live here:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 http://github.com/jcrocholl/pep8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Groups of errors and warnings:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 E errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 W warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 100 indentation
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 200 whitespace
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 300 blank lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 400 imports
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 500 line length
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 600 deprecation
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 700 statements
2801
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
47 900 processing errors
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 You can add checks to this program by writing plugins. Each plugin is
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 a simple function that is called for each line of source code, either
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 physical or logical.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 Physical line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 - Raw line of text from the input file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 Logical line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 - Multi-line statements converted to a single line.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 - Stripped left and right.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 - Contents of strings replaced with 'xxx' of same length.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 - Comments removed.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 The check function requests physical or logical lines by the name of
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 the first argument:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 def maximum_line_length(physical_line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 def extraneous_whitespace(logical_line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 def blank_lines(logical_line, blank_lines, indent_level, line_number)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 The last example above demonstrates how check plugins can request
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 additional information with extra arguments. All attributes of the
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 Checker object are available. Some examples:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 lines: a list of the raw lines from the input file
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 tokens: the tokens that contribute to this logical line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 line_number: line number in the input file
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 blank_lines: blank lines before this one
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 indent_char: first indentation character in this file (' ' or '\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 indent_level: indentation (with tabs expanded to multiples of 8)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 previous_indent_level: indentation on previous line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 previous_logical: previous logical line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 The docstring of each check function shall be the relevant part of
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 text from PEP 8. It is printed if the user enables --show-pep8.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 Several docstrings contain examples directly from the PEP 8 document.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 Okay: spam(ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 E201: spam( ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 These examples are verified automatically when pep8.py is run with the
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 --doctest option. You can add examples for your own check functions.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 The format is simple: "Okay" or error/warning code followed by colon
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 and space, the rest of the line is example source code. If you put 'r'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 before the docstring, you can use \n for newline, \t for tab and \s
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 for space.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 # This is a modified version to make the original tabnanny better suitable
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 # for being called from within the eric5 IDE. The modifications are as
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 # follows:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 # - made messages translatable via Qt
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 #
2302
f29e9405c851 Updated copyright for 2013.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
105 # Copyright (c) 2011 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 #
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 __version__ = '0.6.1'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 import os
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 import sys
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 import re
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 import time
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 import inspect
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 import keyword
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 import tokenize
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 from optparse import OptionParser
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 from fnmatch import fnmatch
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 try:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 frozenset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 except NameError:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 from sets import ImmutableSet as frozenset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 DEFAULT_IGNORE = 'E24'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 MAX_LINE_LENGTH = 79
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 INDENT_REGEX = re.compile(r'([ \t]*)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 RAISE_COMMA_REGEX = re.compile(r'raise\s+\w+\s*(,)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 SELFTEST_REGEX = re.compile(r'(Okay|[EW]\d{3}):\s(.*)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 ERRORCODE_REGEX = re.compile(r'[EW]\d{3}')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 WHITESPACE_AROUND_OPERATOR_REGEX = \
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 re.compile('([^\w\s]*)\s*(\t| )\s*([^\w\s]*)')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 WHITESPACE_AROUND_NAMED_PARAMETER_REGEX = \
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 re.compile(r'[()]|\s=[^=]|[^=!<>]=\s')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 WHITESPACE = ' \t'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 BINARY_OPERATORS = frozenset(['**=', '*=', '+=', '-=', '!=', '<>',
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 '%=', '^=', '&=', '|=', '==', '/=', '//=', '<=', '>=', '<<=', '>>=',
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 '%', '^', '&', '|', '=', '/', '//', '<', '>', '<<'])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 OPERATORS = BINARY_OPERATORS | UNARY_OPERATORS
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 tokenize.DEDENT, tokenize.NEWLINE])
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 E225NOT_KEYWORDS = (frozenset(keyword.kwlist + ['print']) -
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 frozenset(['False', 'None', 'True']))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 BENCHMARK_KEYS = ('directories', 'files', 'logical lines', 'physical lines')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 options = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 args = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 # Plugins (check functions) for physical lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 def tabs_or_spaces(physical_line, indent_char):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 Never mix tabs and spaces.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 The most popular way of indenting Python is with spaces only. The
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 second-most popular way is with tabs only. Code indented with a mixture
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 of tabs and spaces should be converted to using spaces exclusively. When
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 invoking the Python command line interpreter with the -t option, it issues
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 warnings about code that illegally mixes tabs and spaces. When using -tt
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 these warnings become errors. These options are highly recommended!
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 Okay: if a == 0:\n a = 1\n b = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 E101: if a == 0:\n a = 1\n\tb = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 indent = INDENT_REGEX.match(physical_line).group(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 for offset, char in enumerate(indent):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 if char != indent_char:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 return offset, "E101"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 def tabs_obsolete(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 For new projects, spaces-only are strongly recommended over tabs. Most
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 editors have features that make this easy to do.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 Okay: if True:\n return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 W191: if True:\n\treturn
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 indent = INDENT_REGEX.match(physical_line).group(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 if indent.count('\t'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 return indent.index('\t'), "W191"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 def trailing_whitespace(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 JCR: Trailing whitespace is superfluous.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 FBM: Except when it occurs as part of a blank line (i.e. the line is
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 nothing but whitespace). According to Python docs[1] a line with only
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 whitespace is considered a blank line, and is to be ignored. However,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 matching a blank line to its indentation level avoids mistakenly
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 terminating a multi-line statement (e.g. class declaration) when
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 pasting code into the standard Python interpreter.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 [1] http://docs.python.org/reference/lexical_analysis.html#blank-lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 The warning returned varies on whether the line itself is blank, for easier
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 filtering for those who want to indent their blank lines.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 Okay: spam(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 W291: spam(1)\s
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 W293: class Foo(object):\n \n bang = 12
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 physical_line = physical_line.rstrip('\n') # chr(10), newline
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 physical_line = physical_line.rstrip('\r') # chr(13), carriage return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 physical_line = physical_line.rstrip('\x0c') # chr(12), form feed, ^L
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 stripped = physical_line.rstrip()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 if physical_line != stripped:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 if stripped:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 return len(stripped), "W291"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 return 0, "W293"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 def trailing_blank_lines(physical_line, lines, line_number):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 JCR: Trailing blank lines are superfluous.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 Okay: spam(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 W391: spam(1)\n
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 if physical_line.strip() == '' and line_number == len(lines):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 return 0, "W391"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 def missing_newline(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 JCR: The last line should have a newline.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 if physical_line.rstrip() == physical_line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 return len(physical_line), "W292"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 def maximum_line_length(physical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 Limit all lines to a maximum of 79 characters.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 There are still many devices around that are limited to 80 character
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 lines; plus, limiting windows to 80 characters makes it possible to have
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 several windows side-by-side. The default wrapping on such devices looks
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 ugly. Therefore, please limit all lines to a maximum of 79 characters.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 For flowing long blocks of text (docstrings or comments), limiting the
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 length to 72 characters is recommended.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 line = physical_line.rstrip()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 length = len(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 if length > MAX_LINE_LENGTH:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 try:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 # The line could contain multi-byte characters
1145
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
260 if hasattr(line, 'decode'): # Python 2 only
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
261 length = len(line.decode('utf-8'))
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 except UnicodeDecodeError:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 if length > MAX_LINE_LENGTH:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 return MAX_LINE_LENGTH, "E501", length
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 # Plugins (check functions) for logical lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 def blank_lines(logical_line, blank_lines, indent_level, line_number,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 previous_logical, previous_indent_level,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 blank_lines_before_comment):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 Separate top-level function and class definitions with two blank lines.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 Method definitions inside a class are separated by a single blank line.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 Extra blank lines may be used (sparingly) to separate groups of related
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 functions. Blank lines may be omitted between a bunch of related
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 one-liners (e.g. a set of dummy implementations).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 Use blank lines in functions, sparingly, to indicate logical sections.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 Okay: def a():\n pass\n\n\ndef b():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 E301: class Foo:\n b = 0\n def bar():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 E302: def a():\n pass\n\ndef b(n):\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 E303: def a():\n pass\n\n\n\ndef b(n):\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 E303: def a():\n\n\n\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 E304: @decorator\n\ndef a():\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 if line_number == 1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 return # Don't expect blank lines before the first line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 max_blank_lines = max(blank_lines, blank_lines_before_comment)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 if previous_logical.startswith('@'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 if max_blank_lines:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 return 0, "E304"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 elif max_blank_lines > 2 or (indent_level and max_blank_lines == 2):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 return 0, "E303", max_blank_lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 elif (logical_line.startswith('def ') or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 logical_line.startswith('class ') or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 logical_line.startswith('@')):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 if indent_level:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 if not (max_blank_lines or previous_indent_level < indent_level or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 DOCSTRING_REGEX.match(previous_logical)):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 return 0, "E301"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 elif max_blank_lines != 2:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 return 0, "E302", max_blank_lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 def extraneous_whitespace(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 - Immediately inside parentheses, brackets or braces.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 - Immediately before a comma, semicolon, or colon.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 Okay: spam(ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 E201: spam( ham[1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 E201: spam(ham[ 1], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 E201: spam(ham[1], { eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 E202: spam(ham[1], {eggs: 2} )
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 E202: spam(ham[1 ], {eggs: 2})
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 E202: spam(ham[1], {eggs: 2 })
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 E203: if x == 4: print x, y; x, y = y , x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 E203: if x == 4: print x, y ; x, y = y, x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 E203: if x == 4 : print x, y; x, y = y, x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 for match in EXTRANEOUS_WHITESPACE_REGEX.finditer(line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 text = match.group()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 char = text.strip()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 found = match.start()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 if text == char + ' ' and char in '([{':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 return found + 1, "E201", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 if text == ' ' + char and line[found - 1] != ',':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 if char in '}])':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 return found, "E202", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 if char in ',;:':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 return found, "E203", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 def missing_whitespace(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 JCR: Each comma, semicolon or colon should be followed by whitespace.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 Okay: [a, b]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 Okay: (3,)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 Okay: a[1:4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 Okay: a[:4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 Okay: a[1:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 Okay: a[1:4:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 E231: ['a','b']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 E231: foo(bar,baz)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 for index in range(len(line) - 1):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 char = line[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 if char in ',;:' and line[index + 1] not in WHITESPACE:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 before = line[:index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 if char == ':' and before.count('[') > before.count(']'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 continue # Slice syntax, no space required
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 if char == ',' and line[index + 1] == ')':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 continue # Allow tuple with only one element: (3,)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 return index, "E231", char
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 def indentation(logical_line, previous_logical, indent_char,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 indent_level, previous_indent_level):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 Use 4 spaces per indentation level.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 For really old code that you don't want to mess up, you can continue to
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 use 8-space tabs.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 Okay: a = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 Okay: if a == 0:\n a = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 E111: a = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 Okay: for item in items:\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 E112: for item in items:\npass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 Okay: a = 1\nb = 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 E113: a = 1\n b = 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 if indent_char == ' ' and indent_level % 4:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 return 0, "E111"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 indent_expect = previous_logical.endswith(':')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 if indent_expect and indent_level <= previous_indent_level:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 return 0, "E112"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 if indent_level > previous_indent_level and not indent_expect:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 return 0, "E113"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 def whitespace_before_parameters(logical_line, tokens):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 - Immediately before the open parenthesis that starts the argument
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 list of a function call.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 - Immediately before the open parenthesis that starts an indexing or
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 slicing.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 Okay: spam(1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 E211: spam (1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 Okay: dict['key'] = list[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 E211: dict ['key'] = list[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 E211: dict['key'] = list [index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 prev_type = tokens[0][0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 prev_text = tokens[0][1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 prev_end = tokens[0][3]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 for index in range(1, len(tokens)):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 token_type, text, start, end, line = tokens[index]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 if (token_type == tokenize.OP and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 text in '([' and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 start != prev_end and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 (prev_type == tokenize.NAME or prev_text in '}])') and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 # Syntax "class A (B):" is allowed, but avoid it
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 (index < 2 or tokens[index - 2][1] != 'class') and
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 # Allow "return (a.foo for a in range(5))"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 (not keyword.iskeyword(prev_text))):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 return prev_end, "E211", text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 prev_type = token_type
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 prev_text = text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 prev_end = end
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 def whitespace_around_operator(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 - More than one space around an assignment (or other) operator to
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 align it with another.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 Okay: a = 12 + 3
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 E221: a = 4 + 5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 E222: a = 4 + 5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 E223: a = 4\t+ 5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 E224: a = 4 +\t5
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 for match in WHITESPACE_AROUND_OPERATOR_REGEX.finditer(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 before, whitespace, after = match.groups()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 tab = whitespace == '\t'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 offset = match.start(2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 if before in OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 return offset, (tab and "E224" or "E222")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 elif after in OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 return offset, (tab and "E223" or "E221")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 def missing_whitespace_around_operator(logical_line, tokens):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 - Always surround these binary operators with a single space on
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 either side: assignment (=), augmented assignment (+=, -= etc.),
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not),
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 Booleans (and, or, not).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 - Use spaces around arithmetic operators.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 Okay: i = i + 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 Okay: submitted += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 Okay: x = x * 2 - 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 Okay: hypot2 = x * x + y * y
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 Okay: c = (a + b) * (a - b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 Okay: foo(bar, key='word', *args, **kwargs)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 Okay: baz(**kwargs)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 Okay: negative = -1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 Okay: spam(-1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 Okay: alpha[:-i]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 Okay: if not -5 < x < +5:\n pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 Okay: lambda *args, **kw: (args, kw)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 E225: i=i+1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 E225: submitted +=1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 E225: x = x*2 - 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 E225: hypot2 = x*x + y*y
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 E225: c = (a+b) * (a-b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 E225: c = alpha -4
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 E225: z = x **y
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 parens = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 need_space = False
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 prev_type = tokenize.OP
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 prev_text = prev_end = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 for token_type, text, start, end, line in tokens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 if token_type in (tokenize.NL, tokenize.NEWLINE, tokenize.ERRORTOKEN):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 # ERRORTOKEN is triggered by backticks in Python 3000
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 if text in ('(', 'lambda'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 parens += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 elif text == ')':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 parens -= 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 if need_space:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 if start != prev_end:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 need_space = False
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 elif text == '>' and prev_text == '<':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506 # Tolerate the "<>" operator, even if running Python 3
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 return prev_end, "E225"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 elif token_type == tokenize.OP and prev_end is not None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 if text == '=' and parens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 # Allow keyword args or defaults: foo(bar=None).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 pass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 elif text in BINARY_OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 elif text in UNARY_OPERATORS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 # Allow unary operators: -123, -x, +1.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 # Allow argument unpacking: foo(*args, **kwargs).
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 if prev_type == tokenize.OP:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 if prev_text in '}])':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 elif prev_type == tokenize.NAME:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 if prev_text not in E225NOT_KEYWORDS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 need_space = True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 if need_space and start == prev_end:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 return prev_end, "E225"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 prev_type = token_type
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 prev_text = text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 prev_end = end
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 def whitespace_around_comma(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 Avoid extraneous whitespace in the following situations:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 - More than one space around an assignment (or other) operator to
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 align it with another.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 JCR: This should also be applied around comma etc.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 Note: these checks are disabled by default
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 Okay: a = (1, 2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 E241: a = (1, 2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 E242: a = (1,\t2)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 for separator in ',;:':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 found = line.find(separator + ' ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 if found > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 return found + 1, "E241", separator
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 found = line.find(separator + '\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 if found > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 return found + 1, "E242", separator
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 def whitespace_around_named_parameter_equals(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 Don't use spaces around the '=' sign when used to indicate a
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 keyword argument or a default parameter value.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 Okay: def complex(real, imag=0.0):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 Okay: return magic(r=real, i=imag)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 Okay: boolean(a == b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 Okay: boolean(a != b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 Okay: boolean(a <= b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 Okay: boolean(a >= b)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 E251: def complex(real, imag = 0.0):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 E251: return magic(r = real, i = imag)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573 parens = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
574 for match in WHITESPACE_AROUND_NAMED_PARAMETER_REGEX.finditer(
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576 text = match.group()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 if parens and len(text) == 3:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 issue = "E251"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 return match.start(), issue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 if text == '(':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 parens += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 elif text == ')':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 parens -= 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 def whitespace_before_inline_comment(logical_line, tokens):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 Separate inline comments by at least two spaces.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 An inline comment is a comment on the same line as a statement. Inline
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 comments should be separated by at least two spaces from the statement.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 They should start with a # and a single space.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 Okay: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 Okay: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 E261: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 E262: x = x + 1 #Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 E262: x = x + 1 # Increment x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 prev_end = (0, 0)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 for token_type, text, start, end, line in tokens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 if token_type == tokenize.NL:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 if token_type == tokenize.COMMENT:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 if not line[:start[1]].strip():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 if prev_end[0] == start[0] and start[1] < prev_end[1] + 2:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 return (prev_end, "E261")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 if (len(text) > 1 and text.startswith('# ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 or not text.startswith('# ')):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 return start, "E262"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 prev_end = end
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 def imports_on_separate_lines(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 Imports should usually be on separate lines.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 Okay: import os\nimport sys
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 E401: import sys, os
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 Okay: from subprocess import Popen, PIPE
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 Okay: from myclas import MyClass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 Okay: from foo.bar.yourclass import YourClass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 Okay: import myclass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 Okay: import foo.bar.yourclass
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 if line.startswith('import '):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 found = line.find(',')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 if found > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 return found, "E401"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 def compound_statements(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 r"""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 Compound statements (multiple statements on the same line) are
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 generally discouraged.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 While sometimes it's okay to put an if/for/while with a small body
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 on the same line, never do this for multi-clause statements. Also
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 avoid folding such long lines!
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 Okay: if foo == 'blah':\n do_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 Okay: do_one()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 Okay: do_two()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 Okay: do_three()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 E701: if foo == 'blah': do_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 E701: for x in lst: total += x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 E701: while t < 10: t = delay()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 E701: if foo == 'blah': do_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 E701: else: do_non_blah_thing()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 E701: try: something()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 E701: finally: cleanup()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 E701: if foo == 'blah': one(); two(); three()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 E702: do_one(); do_two(); do_three()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 line = logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 found = line.find(':')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 if -1 < found < len(line) - 1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 before = line[:found]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 if (before.count('{') <= before.count('}') and # {'a': 1} (dict)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 before.count('[') <= before.count(']') and # [1:2] (slice)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 not re.search(r'\blambda\b', before)): # lambda x: x
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 return found, "E701"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 found = line.find(';')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 if -1 < found:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 return found, "E702"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 def python_3000_has_key(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 The {}.has_key() method will be removed in the future version of
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 Python. Use the 'in' operation instead, like:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 d = {"a": 1, "b": 2}
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 if "b" in d:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 print d["b"]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 pos = logical_line.find('.has_key(')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 if pos > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 return pos, "W601"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 def python_3000_raise_comma(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 When raising an exception, use "raise ValueError('message')"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 instead of the older form "raise ValueError, 'message'".
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 The paren-using form is preferred because when the exception arguments
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 are long or include string formatting, you don't need to use line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 continuation characters thanks to the containing parentheses. The older
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 form will be removed in Python 3000.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 match = RAISE_COMMA_REGEX.match(logical_line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 if match:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 return match.start(1), "W602"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702 def python_3000_not_equal(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 != can also be written <>, but this is an obsolete usage kept for
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 backwards compatibility only. New code should always use !=.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 The older syntax is removed in Python 3000.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708 pos = logical_line.find('<>')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 if pos > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 return pos, "W603"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 def python_3000_backticks(logical_line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 Backticks are removed in Python 3000.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 Use repr() instead.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 pos = logical_line.find('`')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 if pos > -1:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 return pos, "W604"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 # Helper functions
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 if '' == ''.encode():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 # Python 2: implicit encoding.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 def readlines(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 return open(filename).readlines()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 # Python 3: decode to latin-1.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 # This function is lazy, it does not read the encoding declaration.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 # XXX: use tokenize.detect_encoding()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 def readlines(filename): # __IGNORE_WARNING__
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 return open(filename, encoding='latin-1').readlines()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 def expand_indent(line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 Return the amount of indentation.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 Tabs are expanded to the next multiple of 8.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 >>> expand_indent(' ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746 4
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 >>> expand_indent('\\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 >>> expand_indent(' \\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 >>> expand_indent(' \\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 >>> expand_indent(' \\t')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 16
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 result = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 for char in line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 if char == '\t':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 result = result // 8 * 8 + 8
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 elif char == ' ':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
761 result += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 break
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 return result
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 def mute_string(text):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 Replace contents with 'xxx' to prevent syntax matching.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 >>> mute_string('"abc"')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 '"xxx"'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 >>> mute_string("'''abc'''")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 "'''xxx'''"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 >>> mute_string("r'abc'")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 "r'xxx'"
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 start = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 end = len(text) - 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 # String modifiers (e.g. u or r)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 if text.endswith('"'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 start += text.index('"')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 elif text.endswith("'"):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 start += text.index("'")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 # Triple quotes
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 if text.endswith('"""') or text.endswith("'''"):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 start += 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 end -= 2
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 return text[:start] + 'x' * (end - start) + text[end:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 def message(text):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 """Print a message."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 # print >> sys.stderr, options.prog + ': ' + text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 # print >> sys.stderr, text
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 print(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 # Framework to run all checks
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 ##############################################################################
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 def find_checks(argument_name):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 Find all globally visible functions where the first argument name
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 starts with argument_name.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809 checks = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 for name, function in globals().items():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 if not inspect.isfunction(function):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 args = inspect.getargspec(function)[0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
814 if args and args[0].startswith(argument_name):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815 codes = ERRORCODE_REGEX.findall(inspect.getdoc(function) or '')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
816 for code in codes or ['']:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 if not code or not ignore_code(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 checks.append((name, function, args))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819 break
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 checks.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 return checks
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824 class Checker(object):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 Load a Python source file, tokenize it, check coding style.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829 def __init__(self, filename, lines=None):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 self.filename = filename
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831 if filename is None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 self.filename = 'stdin'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 self.lines = lines or []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 elif lines is None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835 self.lines = readlines(filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 self.lines = lines
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
838 options.counters['physical lines'] += len(self.lines)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
839
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840 def readline(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 Get the next line from the input buffer.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
843 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844 self.line_number += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845 if self.line_number > len(self.lines):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846 return ''
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
847 return self.lines[self.line_number - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
849 def readline_check_physical(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851 Check and return the next physical line. This method can be
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 used to feed tokenize.generate_tokens.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 line = self.readline()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 if line:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 self.check_physical(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857 return line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
858
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 def run_check(self, check, argument_names):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 Run a check plugin.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 arguments = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 for name in argument_names:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 arguments.append(getattr(self, name))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866 return check(*arguments)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
867
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
868 def check_physical(self, line):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
869 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
870 Run all physical checks on a raw input line.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
872 self.physical_line = line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
873 if self.indent_char is None and len(line) and line[0] in ' \t':
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
874 self.indent_char = line[0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
875 for name, check, argument_names in options.physical_checks:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
876 result = self.run_check(check, argument_names)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
877 if result is not None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
878 offset, code = result[:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
879 args = result[2:]
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 842
diff changeset
880 self.report_error_args(self.line_number, offset, code, check,
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
881 *args)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
882
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
883 def build_tokens_line(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
884 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
885 Build a logical line from tokens.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
886 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
887 self.mapping = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
888 logical = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
889 length = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
890 previous = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
891 for token in self.tokens:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
892 token_type, text = token[0:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
893 if token_type in SKIP_TOKENS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 if token_type == tokenize.STRING:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
896 text = mute_string(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 if previous:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
898 end_line, end = previous[3]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
899 start_line, start = token[2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
900 if end_line != start_line: # different row
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
901 prev_text = self.lines[end_line - 1][end - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
902 if prev_text == ',' or (prev_text not in '{[('
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
903 and text not in '}])'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
904 logical.append(' ')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
905 length += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
906 elif end != start: # different column
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
907 fill = self.lines[end_line - 1][end:start]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
908 logical.append(fill)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
909 length += len(fill)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
910 self.mapping.append((length, token))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
911 logical.append(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
912 length += len(text)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 previous = token
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914 self.logical_line = ''.join(logical)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915 assert self.logical_line.lstrip() == self.logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916 assert self.logical_line.rstrip() == self.logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
918 def check_logical(self):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
920 Build a line from tokens and run all logical checks on it.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
921 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 options.counters['logical lines'] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923 self.build_tokens_line()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924 first_line = self.lines[self.mapping[0][1][2][0] - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 indent = first_line[:self.mapping[0][1][2][1]]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
926 self.previous_indent_level = self.indent_level
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927 self.indent_level = expand_indent(indent)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 if options.verbose >= 2:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 print(self.logical_line[:80].rstrip())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930 for name, check, argument_names in options.logical_checks:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
931 if options.verbose >= 4:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 print(' ' + name)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 result = self.run_check(check, argument_names)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 if result is not None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 offset, code = result[:2]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 args = result[2:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 if isinstance(offset, tuple):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 original_number, original_offset = offset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 for token_offset, token in self.mapping:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 if offset >= token_offset:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942 original_number = token[2][0]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
943 original_offset = (token[2][1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944 + offset - token_offset)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 self.report_error_args(original_number, original_offset,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
946 code, check, *args)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
947 self.previous_logical = self.logical_line
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 def check_all(self, expected=None, line_offset=0):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 Run all checks on the input file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
952 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953 self.expected = expected or ()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 self.line_offset = line_offset
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
955 self.line_number = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
956 self.file_errors = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
957 self.indent_char = None
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 self.indent_level = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
959 self.previous_logical = ''
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
960 self.blank_lines = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
961 self.blank_lines_before_comment = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
962 self.tokens = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
963 parens = 0
2801
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
964 try:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
965 for token in tokenize.generate_tokens(self.readline_check_physical):
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
966 if options.verbose >= 3:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
967 if token[2][0] == token[3][0]:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
968 pos = '[%s:%s]' % (token[2][1] or '', token[3][1])
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
969 else:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
970 pos = 'l.%s' % token[3][0]
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
971 print('l.%s\t%s\t%s\t%r' %
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
972 (token[2][0], pos, tokenize.tok_name[token[0]], token[1]))
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
973 self.tokens.append(token)
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
974 token_type, text = token[0:2]
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
975 if token_type == tokenize.OP and text in '([{':
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
976 parens += 1
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
977 if token_type == tokenize.OP and text in '}])':
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
978 parens -= 1
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
979 if token_type == tokenize.NEWLINE and not parens:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
980 self.check_logical()
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
981 self.blank_lines = 0
2801
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
982 self.blank_lines_before_comment = 0
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
983 self.tokens = []
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
984 if token_type == tokenize.NL and not parens:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
985 if len(self.tokens) <= 1:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
986 # The physical line contains only this token.
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
987 self.blank_lines += 1
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
988 self.tokens = []
2801
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
989 if token_type == tokenize.COMMENT:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
990 source_line = token[4]
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
991 token_start = token[2][1]
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
992 if source_line[:token_start].strip() == '':
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
993 self.blank_lines_before_comment = max(self.blank_lines,
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
994 self.blank_lines_before_comment)
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
995 self.blank_lines = 0
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
996 if text.endswith('\n') and not parens:
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
997 # The comment also ends a physical line. This works around
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
998 # Python < 2.6 behaviour, which does not generate NL after
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
999 # a comment which is on a line by itself.
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1000 self.tokens = []
2931
552d5934c6f6 Fixed an error in the pep8 checkers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2801
diff changeset
1001 except (SyntaxError, tokenize.TokenError) as err:
2801
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1002 msg, (lnum, pos) = err.args
8ccc38b80dc2 Fixed an issue in the pep8 checker tokenising a bad file.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1003 self.report_error_args(lnum, pos, "E901", "TokenError", msg)
842
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1004 return self.file_errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1005
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1006 def report_error(self, line_number, offset, text, check):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1007 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1008 Report an error, according to options.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1009 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1010 code = text[:4]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1011 if ignore_code(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1012 return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1013 if options.quiet == 1 and not self.file_errors:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 message(self.filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1015 if code in options.counters:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1016 options.counters[code] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1017 else:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1018 options.counters[code] = 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1019 options.messages[code] = text[5:]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1020 if options.quiet or code in self.expected:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1021 # Don't care about expected errors or warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1022 return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1023 self.file_errors += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1024 if options.counters[code] == 1 or options.repeat:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1025 message("%s:%s:%d: %s" %
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1026 (self.filename, self.line_offset + line_number,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1027 offset + 1, text))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1028 if options.show_source:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1029 line = self.lines[line_number - 1]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1030 message(line.rstrip())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1031 message(' ' * offset + '^')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1032 if options.show_pep8:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1033 message(check.__doc__.lstrip('\n').rstrip())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1034
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1035
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1036 def input_file(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1037 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1038 Run all checks on a Python source file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1039 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1040 if options.verbose:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1041 message('checking ' + filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042 Checker(filename).check_all()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045 def input_dir(dirname, runner=None):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1047 Check all Python source files in this directory and all subdirectories.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 dirname = dirname.rstrip('/')
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1050 if excluded(dirname):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1051 return
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1052 if runner is None:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1053 runner = input_file
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1054 for root, dirs, files in os.walk(dirname):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1055 if options.verbose:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1056 message('directory ' + root)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1057 options.counters['directories'] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1058 dirs.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1059 for subdir in dirs:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1060 if excluded(subdir):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1061 dirs.remove(subdir)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1062 files.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1063 for filename in files:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1064 if filename_match(filename) and not excluded(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1065 options.counters['files'] += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1066 runner(os.path.join(root, filename))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1067
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1068
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1069 def excluded(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1070 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1071 Check if options.exclude contains a pattern that matches filename.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1072 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1073 basename = os.path.basename(filename)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1074 for pattern in options.exclude:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1075 if fnmatch(basename, pattern):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1076 # print basename, 'excluded because it matches', pattern
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1077 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1078
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1079
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1080 def filename_match(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1081 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1082 Check if options.filename contains a pattern that matches filename.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1083 If options.filename is unspecified, this always returns True.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1084 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1085 if not options.filename:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1086 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1087 for pattern in options.filename:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1088 if fnmatch(filename, pattern):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1089 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1090
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1091
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1092 def ignore_code(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1093 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1094 Check if options.ignore contains a prefix of the error code.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1095 If options.select contains a prefix of the error code, do not ignore it.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1096 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1097 for select in options.select:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1098 if code.startswith(select):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1099 return False
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1100 for ignore in options.ignore:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1101 if code.startswith(ignore):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1102 return True
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1103
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1104
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1105 def reset_counters():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1106 for key in list(options.counters.keys()):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1107 if key not in BENCHMARK_KEYS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1108 del options.counters[key]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1109 options.messages = {}
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1110
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1111
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1112 def get_error_statistics():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1113 """Get error statistics."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1114 return get_statistics("E")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1115
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1116
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1117 def get_warning_statistics():
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1118 """Get warning statistics."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1119 return get_statistics("W")
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1120
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1121
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1122 def get_statistics(prefix=''):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1123 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1124 Get statistics for message codes that start with the prefix.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1125
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1126 prefix='' matches all errors and warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1127 prefix='E' matches all errors
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1128 prefix='W' matches all warnings
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1129 prefix='E4' matches all errors that have to do with imports
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1130 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1131 stats = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1132 keys = list(options.messages.keys())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1133 keys.sort()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1134 for key in keys:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1135 if key.startswith(prefix):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1136 stats.append('%-7s %s %s' %
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1137 (options.counters[key], key, options.messages[key]))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1138 return stats
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1139
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1140
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1141 def get_count(prefix=''):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1142 """Return the total count of errors and warnings."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1143 keys = list(options.messages.keys())
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1144 count = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1145 for key in keys:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1146 if key.startswith(prefix):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1147 count += options.counters[key]
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1148 return count
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1149
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1150
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1151 def print_statistics(prefix=''):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1152 """Print overall statistics (number of errors and warnings)."""
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1153 for line in get_statistics(prefix):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1154 print(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1155
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1156
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1157 def print_benchmark(elapsed):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1158 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1159 Print benchmark numbers.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1160 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1161 print('%-7.2f %s' % (elapsed, 'seconds elapsed'))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1162 for key in BENCHMARK_KEYS:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1163 print('%-7d %s per second (%d total)' % (
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1164 options.counters[key] / elapsed, key,
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1165 options.counters[key]))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1166
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1167
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1168 def run_tests(filename):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1169 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1170 Run all the tests from a file.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1171
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1172 A test file can provide many tests. Each test starts with a declaration.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1173 This declaration is a single line starting with '#:'.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1174 It declares codes of expected failures, separated by spaces or 'Okay'
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1175 if no failure is expected.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1176 If the file does not contain such declaration, it should pass all tests.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1177 If the declaration is empty, following lines are not checked, until next
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1178 declaration.
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1179
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1180 Examples:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1181
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1182 * Only E224 and W701 are expected: #: E224 W701
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1183 * Following example is conform: #: Okay
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1184 * Don't check these lines: #:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1185 """
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1186 lines = readlines(filename) + ['#:\n']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1187 line_offset = 0
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1188 codes = ['Okay']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1189 testcase = []
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1190 for index, line in enumerate(lines):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1191 if not line.startswith('#:'):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1192 if codes:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1193 # Collect the lines of the test case
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1194 testcase.append(line)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1195 continue
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1196 if codes and index > 0:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1197 label = '%s:%s:1' % (filename, line_offset + 1)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1198 codes = [c for c in codes if c != 'Okay']
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1199 # Run the checker
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1200 errors = Checker(filename, testcase).check_all(codes, line_offset)
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1201 # Check if the expected errors were found
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1202 for code in codes:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1203 if not options.counters.get(code):
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1204 errors += 1
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1205 message('%s: error %s not found' % (label, code))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1206 if options.verbose and not errors:
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1207 message('%s: passed (%s)' % (label, ' '.join(codes)))
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1208 # Keep showing errors for multiple tests
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1209 reset_counters()
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1210 # output the real line numbers
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1211 line_offset = index
984b5535cd26 Started to add the PEP 8 checker for Python2.
Detlev Offenbach <detlev@die-offenbach&#