Plugins/CheckerPlugins/CodeStyleChecker/pep8.py

Thu, 29 May 2014 21:02:44 +0200

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Thu, 29 May 2014 21:02:44 +0200
changeset 3616
081b69e00f3e
parent 3484
645c12de6b0c
child 3617
2f859c5dcfb4
permissions
-rw-r--r--

updated pep8 to 1.5.6

3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1 #!/usr/bin/env python
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 # pep8.py - Check Python source code formatting, according to PEP 8
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
5 # Copyright (C) 2006-2009 Johann C. Rocholl <johann@rocholl.net>
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
6 # Copyright (C) 2009-2014 Florent Xicluna <florent.xicluna@gmail.com>
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 # Permission is hereby granted, free of charge, to any person
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # obtaining a copy of this software and associated documentation files
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 # (the "Software"), to deal in the Software without restriction,
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # including without limitation the rights to use, copy, modify, merge,
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # publish, distribute, sublicense, and/or sell copies of the Software,
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 # and to permit persons to whom the Software is furnished to do so,
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 # subject to the following conditions:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 # The above copyright notice and this permission notice shall be
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 # included in all copies or substantial portions of the Software.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 # SOFTWARE.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
28 r"""
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
29 Check Python source code formatting, according to PEP 8.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 For usage and a list of options, try this:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 $ python pep8.py -h
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 This program and its regression test suite live here:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 http://github.com/jcrocholl/pep8
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Groups of errors and warnings:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 E errors
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 W warnings
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 100 indentation
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 200 whitespace
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 300 blank lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 400 imports
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 500 line length
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 600 deprecation
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 700 statements
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
47 900 syntax error
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 """
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
49 from __future__ import with_statement
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 #
2861
cdcbca0cea82 Fixed a typo in the included pep8.py modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2799
diff changeset
52 # This is a modified version to make the original pep8.py better suitable
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 # for being called from within the eric5 IDE. The modifications are as
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 # follows:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 # - made messages translatable via Qt
2864
d973dab8b715 Upgraded the Py2 part of the pep8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2863
diff changeset
57 # - added code for eric5 integration
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 #
3160
209a07d7e401 Updated copyright for 2014.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
59 # Copyright (c) 2011 - 2014 Detlev Offenbach <detlev@die-offenbachs.de>
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
62 __version__ = '1.5.6'
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 import os
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 import sys
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 import re
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 import time
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 import inspect
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 import keyword
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 import tokenize
3210
8f4fe6f76729 Fixed some dubious code related to AST generation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
71 import ast
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 from optparse import OptionParser
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 from fnmatch import fnmatch
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 try:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
75 from configparser import RawConfigParser
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
76 from io import TextIOWrapper
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
77 except ImportError:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
78 from ConfigParser import RawConfigParser # __IGNORE_WARNING__
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
80 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
81 DEFAULT_IGNORE = 'E123,E226,E24'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
82 if sys.platform == 'win32':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
83 DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
84 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
85 DEFAULT_CONFIG = os.path.join(os.getenv('XDG_CONFIG_HOME') or
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
86 os.path.expanduser('~/.config'), 'pep8')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
87 PROJECT_CONFIG = ('setup.cfg', 'tox.ini', '.pep8')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
88 TESTSUITE_PATH = os.path.join(os.path.dirname(__file__), 'testsuite')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 MAX_LINE_LENGTH = 79
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
90 REPORT_FORMAT = {
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
91 'default': '%(path)s:%(row)d:%(col)d: %(code)s %(text)s',
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
92 'pylint': '%(path)s:%(row)d: [%(code)s] %(text)s',
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
93 }
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
94
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
95 SINGLETONS = frozenset(['False', 'None', 'True'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
96 KEYWORDS = frozenset(keyword.kwlist + ['print']) - SINGLETONS
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
97 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
98 ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
99 WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
100 WS_NEEDED_OPERATORS = frozenset([
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
101 '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
102 '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
103 WHITESPACE = frozenset(' \t')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
104 NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
105 SKIP_TOKENS = NEWLINE.union([tokenize.INDENT, tokenize.DEDENT])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
106 # ERRORTOKEN is triggered by backticks in Python 3
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
107 SKIP_COMMENTS = SKIP_TOKENS.union([tokenize.COMMENT, tokenize.ERRORTOKEN])
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
108 BENCHMARK_KEYS = ['directories', 'files', 'logical lines', 'physical lines']
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 INDENT_REGEX = re.compile(r'([ \t]*)')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
111 RAISE_COMMA_REGEX = re.compile(r'raise\s+\w+\s*,')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
112 RERAISE_COMMA_REGEX = re.compile(r'raise\s+\w+\s*,.*,\s*\w+\s*$')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
113 ERRORCODE_REGEX = re.compile(r'\b[A-Z]\d{3}\b')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
116 WHITESPACE_AFTER_COMMA_REGEX = re.compile(r'[,;:]\s*(?: |\t)')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
117 COMPARE_SINGLETON_REGEX = re.compile(r'([=!]=)\s*(None|False|True)')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
118 COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+[^[({ ]+\s+(in|is)\s')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
119 COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
120 r'|\s*\(\s*([^)]*[^ )])\s*\))')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
121 KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
122 OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+)(\s*)')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
123 LAMBDA_REGEX = re.compile(r'\blambda\b')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
124 HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
126 # Work around Python < 2.6 behaviour, which does not generate NL after
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
127 # a comment which is on a line by itself.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
128 COMMENT_WITH_NL = tokenize.generate_tokens(['#\n'].pop).send(None)[1] == '#\n'
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 # Plugins (check functions) for physical lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 def tabs_or_spaces(physical_line, indent_char):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
137 r"""Never mix tabs and spaces.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 The most popular way of indenting Python is with spaces only. The
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 second-most popular way is with tabs only. Code indented with a mixture
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 of tabs and spaces should be converted to using spaces exclusively. When
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 invoking the Python command line interpreter with the -t option, it issues
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 warnings about code that illegally mixes tabs and spaces. When using -tt
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 these warnings become errors. These options are highly recommended!
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 Okay: if a == 0:\n a = 1\n b = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 E101: if a == 0:\n a = 1\n\tb = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 indent = INDENT_REGEX.match(physical_line).group(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 for offset, char in enumerate(indent):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 if char != indent_char:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
152 return offset, "E101 indentation contains mixed spaces and tabs"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 def tabs_obsolete(physical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
156 r"""For new projects, spaces-only are strongly recommended over tabs.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 Okay: if True:\n return
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 W191: if True:\n\treturn
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 indent = INDENT_REGEX.match(physical_line).group(1)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
162 if '\t' in indent:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
163 return indent.index('\t'), "W191 indentation contains tabs"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 def trailing_whitespace(physical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
167 r"""Trailing whitespace is superfluous.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 The warning returned varies on whether the line itself is blank, for easier
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 filtering for those who want to indent their blank lines.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
172 Okay: spam(1)\n#
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
173 W291: spam(1) \n#
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 W293: class Foo(object):\n \n bang = 12
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 physical_line = physical_line.rstrip('\n') # chr(10), newline
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 physical_line = physical_line.rstrip('\r') # chr(13), carriage return
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 physical_line = physical_line.rstrip('\x0c') # chr(12), form feed, ^L
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
179 stripped = physical_line.rstrip(' \t\v')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 if physical_line != stripped:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 if stripped:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
182 return len(stripped), "W291 trailing whitespace"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
184 return 0, "W293 blank line contains whitespace"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
187 def trailing_blank_lines(physical_line, lines, line_number, total_lines):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
188 r"""Trailing blank lines are superfluous.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 Okay: spam(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 W391: spam(1)\n
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
192
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
193 However the last line should end with a new line (warning W292).
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 """
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
195 if line_number == total_lines:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
196 stripped_last_line = physical_line.rstrip()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
197 if not stripped_last_line:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
198 return 0, "W391 blank line at end of file"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
199 if stripped_last_line == physical_line:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
200 return len(physical_line), "W292 no newline at end of file"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
203 def maximum_line_length(physical_line, max_line_length, multiline):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
204 r"""Limit all lines to a maximum of 79 characters.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 There are still many devices around that are limited to 80 character
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 lines; plus, limiting windows to 80 characters makes it possible to have
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 several windows side-by-side. The default wrapping on such devices looks
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 ugly. Therefore, please limit all lines to a maximum of 79 characters.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 For flowing long blocks of text (docstrings or comments), limiting the
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 length to 72 characters is recommended.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
212
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
213 Reports error E501.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 line = physical_line.rstrip()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 length = len(line)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
217 if length > max_line_length and not noqa(line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
218 # Special case for long URLs in multi-line docstrings or comments,
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
219 # but still report the error when the 72 first chars are whitespaces.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
220 chunks = line.split()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
221 if ((len(chunks) == 1 and multiline) or
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
222 (len(chunks) == 2 and chunks[0] == '#')) and \
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
223 len(line) - len(chunks[-1]) < max_line_length - 7:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
224 return
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
225 if hasattr(line, 'decode'): # Python 2
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 # The line could contain multi-byte characters
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
227 try:
1145
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
228 length = len(line.decode('utf-8'))
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
229 except UnicodeError:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
230 pass
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
231 if length > max_line_length:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
232 return (max_line_length, "E501 line too long ", length, max_line_length)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 # Plugins (check functions) for logical lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 def blank_lines(logical_line, blank_lines, indent_level, line_number,
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
241 blank_before, previous_logical, previous_indent_level):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
242 r"""Separate top-level function and class definitions with two blank lines.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 Method definitions inside a class are separated by a single blank line.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 Extra blank lines may be used (sparingly) to separate groups of related
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 functions. Blank lines may be omitted between a bunch of related
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 one-liners (e.g. a set of dummy implementations).
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 Use blank lines in functions, sparingly, to indicate logical sections.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 Okay: def a():\n pass\n\n\ndef b():\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 E301: class Foo:\n b = 0\n def bar():\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 E302: def a():\n pass\n\ndef b(n):\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 E303: def a():\n pass\n\n\n\ndef b(n):\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 E303: def a():\n\n\n\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 E304: @decorator\n\ndef a():\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
261 if line_number < 3 and not previous_logical:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 return # Don't expect blank lines before the first line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 if previous_logical.startswith('@'):
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
264 if blank_lines:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
265 yield 0, "E304 blank lines found after function decorator"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
266 elif blank_lines > 2 or (indent_level and blank_lines == 2):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
267 yield 0, "E303 too many blank lines (%d)", blank_lines
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
268 elif logical_line.startswith(('def ', 'class ', '@')):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 if indent_level:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
270 if not (blank_before or previous_indent_level < indent_level or
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 DOCSTRING_REGEX.match(previous_logical)):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
272 yield 0, "E301 expected 1 blank line, found 0"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
273 elif blank_before != 2:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
274 yield 0, "E302 expected 2 blank lines, found %d", blank_before
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 def extraneous_whitespace(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
278 r"""Avoid extraneous whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
280 Avoid extraneous whitespace in these situations:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 - Immediately inside parentheses, brackets or braces.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 - Immediately before a comma, semicolon, or colon.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 Okay: spam(ham[1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 E201: spam( ham[1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 E201: spam(ham[ 1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 E201: spam(ham[1], { eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 E202: spam(ham[1], {eggs: 2} )
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 E202: spam(ham[1 ], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 E202: spam(ham[1], {eggs: 2 })
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 E203: if x == 4: print x, y; x, y = y , x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 E203: if x == 4: print x, y ; x, y = y, x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 E203: if x == 4 : print x, y; x, y = y, x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 for match in EXTRANEOUS_WHITESPACE_REGEX.finditer(line):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 text = match.group()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 char = text.strip()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 found = match.start()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
301 if text == char + ' ':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
302 # assert char in '([{'
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
303 yield found + 1, "E201 whitespace after '%s'", char
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
304 elif line[found - 1] != ',':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
305 code = ('E202' if char in '}])' else 'E203') # if char in ',;:'
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
306 yield found, "%s whitespace before '%s'" % (code, char), char
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
307
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
308
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
309 def whitespace_around_keywords(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
310 r"""Avoid extraneous whitespace around keywords.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
311
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
312 Okay: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
313 E271: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
314 E272: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
315 E273: True and\tFalse
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
316 E274: True\tand False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
317 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
318 for match in KEYWORD_REGEX.finditer(logical_line):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
319 before, after = match.groups()
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
320
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
321 if '\t' in before:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
322 yield match.start(1), "E274 tab before keyword"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
323 elif len(before) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
324 yield match.start(1), "E272 multiple spaces before keyword"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
325
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
326 if '\t' in after:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
327 yield match.start(2), "E273 tab after keyword"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
328 elif len(after) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
329 yield match.start(2), "E271 multiple spaces after keyword"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 def missing_whitespace(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
333 r"""Each comma, semicolon or colon should be followed by whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 Okay: [a, b]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 Okay: (3,)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 Okay: a[1:4]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 Okay: a[:4]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 Okay: a[1:]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 Okay: a[1:4:2]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 E231: ['a','b']
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 E231: foo(bar,baz)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
343 E231: [{'a':'b'}]
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 for index in range(len(line) - 1):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 char = line[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 if char in ',;:' and line[index + 1] not in WHITESPACE:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 before = line[:index]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
350 if char == ':' and before.count('[') > before.count(']') and \
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
351 before.rfind('{') < before.rfind('['):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 continue # Slice syntax, no space required
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 if char == ',' and line[index + 1] == ')':
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 continue # Allow tuple with only one element: (3,)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
355 yield index, "E231 missing whitespace after '%s'", char
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 def indentation(logical_line, previous_logical, indent_char,
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 indent_level, previous_indent_level):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
360 r"""Use 4 spaces per indentation level.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 For really old code that you don't want to mess up, you can continue to
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 use 8-space tabs.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 Okay: a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 Okay: if a == 0:\n a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 E111: a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 Okay: for item in items:\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 E112: for item in items:\npass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 Okay: a = 1\nb = 2
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 E113: a = 1\n b = 2
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 if indent_char == ' ' and indent_level % 4:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
376 yield 0, "E111 indentation is not a multiple of four"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 indent_expect = previous_logical.endswith(':')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 if indent_expect and indent_level <= previous_indent_level:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
379 yield 0, "E112 expected an indented block"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 if indent_level > previous_indent_level and not indent_expect:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
381 yield 0, "E113 unexpected indentation"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
382
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
383
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
384 def continued_indentation(logical_line, tokens, indent_level, hang_closing,
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
385 indent_char, noqa, verbose):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
386 r"""Continuation lines indentation.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
387
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
388 Continuation lines should align wrapped elements either vertically
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
389 using Python's implicit line joining inside parentheses, brackets
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
390 and braces, or using a hanging indent.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
391
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
392 When using a hanging indent these considerations should be applied:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
393 - there should be no arguments on the first line, and
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
394 - further indentation should be used to clearly distinguish itself as a
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
395 continuation line.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
396
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
397 Okay: a = (\n)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
398 E123: a = (\n )
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
399
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
400 Okay: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
401 E121: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
402 E122: a = (\n42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
403 E123: a = (\n 42\n )
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
404 E124: a = (24,\n 42\n)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
405 E125: if (\n b):\n pass
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
406 E126: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
407 E127: a = (24,\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
408 E128: a = (24,\n 42)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
409 E129: if (a or\n b):\n pass
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
410 E131: a = (\n 42\n 24)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
411 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
412 first_row = tokens[0][2][0]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
413 nrows = 1 + tokens[-1][2][0] - first_row
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
414 if noqa or nrows == 1:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
415 return
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
416
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
417 # indent_next tells us whether the next block is indented; assuming
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
418 # that it is indented by 4 spaces, then we should not allow 4-space
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
419 # indents on the final continuation line; in turn, some other
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
420 # indents are allowed to have an extra 4 spaces.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
421 indent_next = logical_line.endswith(':')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
422
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
423 row = depth = 0
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
424 valid_hangs = (4,) if indent_char != '\t' else (4, 8)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
425 # remember how many brackets were opened on each line
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
426 parens = [0] * nrows
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
427 # relative indents of physical lines
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
428 rel_indent = [0] * nrows
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
429 # for each depth, collect a list of opening rows
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
430 open_rows = [[0]]
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
431 # for each depth, memorize the hanging indentation
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
432 hangs = [None]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
433 # visual indents
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
434 indent_chances = {}
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
435 last_indent = tokens[0][2]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
436 visual_indent = None
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
437 # for each depth, memorize the visual indent column
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
438 indent = [last_indent[1]]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
439 if verbose >= 3:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
440 print(">>> " + tokens[0][4].rstrip())
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
441
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
442 for token_type, text, start, end, line in tokens:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
443
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
444 last_token_multiline = (start[0] != end[0])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
445 newline = row < start[0] - first_row
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
446 if newline:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
447 row = start[0] - first_row
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
448 newline = not last_token_multiline and token_type not in NEWLINE
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
449
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
450 if newline:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
451 # this is the beginning of a continuation line.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
452 last_indent = start
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
453 if verbose >= 3:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
454 print("... " + line.rstrip())
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
455
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
456 # record the initial indent.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
457 rel_indent[row] = expand_indent(line) - indent_level
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
458
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
459 # identify closing bracket
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
460 close_bracket = (token_type == tokenize.OP and text in ']})')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
461
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
462 # is the indent relative to an opening bracket line?
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
463 for open_row in reversed(open_rows[depth]):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
464 hang = rel_indent[row] - rel_indent[open_row]
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
465 hanging_indent = hang in valid_hangs
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
466 if hanging_indent:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
467 break
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
468 if hangs[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
469 hanging_indent = (hang == hangs[depth])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
470 # is there any chance of visual indent?
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
471 visual_indent = (not close_bracket and hang > 0 and
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
472 indent_chances.get(start[1]))
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
473
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
474 if close_bracket and indent[depth]:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
475 # closing bracket for visual indent
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
476 if start[1] != indent[depth]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
477 yield (start, "E124 closing bracket does not match "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
478 "visual indentation")
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
479 elif close_bracket and not hang:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
480 # closing bracket matches indentation of opening bracket's line
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
481 if hang_closing:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
482 yield start, "E133 closing bracket is missing indentation"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
483 elif indent[depth] and start[1] < indent[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
484 if visual_indent is not True:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
485 # visual indent is broken
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
486 yield (start, "E128 continuation line "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
487 "under-indented for visual indent")
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
488 elif hanging_indent or (indent_next and rel_indent[row] == 8):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
489 # hanging indent is verified
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
490 if close_bracket and not hang_closing:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
491 yield (start, "E123 closing bracket does not match "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
492 "indentation of opening bracket's line")
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
493 hangs[depth] = hang
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
494 elif visual_indent is True:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
495 # visual indent is verified
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
496 indent[depth] = start[1]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
497 elif visual_indent in (text, str):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
498 # ignore token lined up with matching one from a previous line
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
499 pass
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
500 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
501 # indent is broken
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
502 if hang <= 0:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
503 error = "E122", "missing indentation or outdented"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
504 elif indent[depth]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
505 error = "E127", "over-indented for visual indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
506 elif not close_bracket and hangs[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
507 error = "E131", "unaligned for hanging indent"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
508 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
509 hangs[depth] = hang
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
510 if hang > 4:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
511 error = "E126", "over-indented for hanging indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
512 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
513 error = "E121", "under-indented for hanging indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
514 yield start, "%s continuation line %s" % error
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
515
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
516 # look for visual indenting
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
517 if (parens[row] and token_type not in (tokenize.NL, tokenize.COMMENT)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
518 and not indent[depth]):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
519 indent[depth] = start[1]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
520 indent_chances[start[1]] = True
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
521 if verbose >= 4:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
522 print("bracket depth %s indent to %s" % (depth, start[1]))
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
523 # deal with implicit string concatenation
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
524 elif (token_type in (tokenize.STRING, tokenize.COMMENT) or
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
525 text in ('u', 'ur', 'b', 'br')):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
526 indent_chances[start[1]] = str
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
527 # special case for the "if" statement because len("if (") == 4
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
528 elif not indent_chances and not row and not depth and text == 'if':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
529 indent_chances[end[1] + 1] = True
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
530 elif text == ':' and line[end[1]:].isspace():
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
531 open_rows[depth].append(row)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
532
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
533 # keep track of bracket depth
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
534 if token_type == tokenize.OP:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
535 if text in '([{':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
536 depth += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
537 indent.append(0)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
538 hangs.append(None)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
539 if len(open_rows) == depth:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
540 open_rows.append([])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
541 open_rows[depth].append(row)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
542 parens[row] += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
543 if verbose >= 4:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
544 print("bracket depth %s seen, col %s, visual min = %s" %
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
545 (depth, start[1], indent[depth]))
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
546 elif text in ')]}' and depth > 0:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
547 # parent indents should not be more than this one
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
548 prev_indent = indent.pop() or last_indent[1]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
549 hangs.pop()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
550 for d in range(depth):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
551 if indent[d] > prev_indent:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
552 indent[d] = 0
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
553 for ind in list(indent_chances):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
554 if ind >= prev_indent:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
555 del indent_chances[ind]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
556 del open_rows[depth + 1:]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
557 depth -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
558 if depth:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
559 indent_chances[indent[depth]] = True
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
560 for idx in range(row, -1, -1):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
561 if parens[idx]:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
562 parens[idx] -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
563 break
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
564 assert len(indent) == depth + 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
565 if start[1] not in indent_chances:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
566 # allow to line up tokens
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
567 indent_chances[start[1]] = text
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
568
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
569 if last_token_multiline:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
570 rel_indent[end[0] - first_row] = rel_indent[row]
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
571
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
572 if indent_next and expand_indent(line) == indent_level + 4:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
573 pos = (start[0], indent[0] + 4)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
574 if visual_indent:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
575 code = "E129 visually indented line"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
576 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
577 code = "E125 continuation line"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
578 yield pos, "%s with same indent as next logical line" % code
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 def whitespace_before_parameters(logical_line, tokens):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
582 r"""Avoid extraneous whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
584 Avoid extraneous whitespace in the following situations:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
585 - before the open parenthesis that starts the argument list of a
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
586 function call.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
587 - before the open parenthesis that starts an indexing or slicing.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 Okay: spam(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 E211: spam (1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 Okay: dict['key'] = list[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593 E211: dict ['key'] = list[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 E211: dict['key'] = list [index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
596 prev_type, prev_text, __, prev_end, __ = tokens[0]
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 for index in range(1, len(tokens)):
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
598 token_type, text, start, end, __ = tokens[index]
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 if (token_type == tokenize.OP and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 text in '([' and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 start != prev_end and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 (prev_type == tokenize.NAME or prev_text in '}])') and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 # Syntax "class A (B):" is allowed, but avoid it
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 (index < 2 or tokens[index - 2][1] != 'class') and
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
605 # Allow "return (a.foo for a in range(5))"
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
606 not keyword.iskeyword(prev_text)):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
607 yield prev_end, "E211 whitespace before '%s'", text
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 prev_type = token_type
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 prev_text = text
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613 def whitespace_around_operator(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
614 r"""Avoid extraneous whitespace around an operator.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 Okay: a = 12 + 3
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 E221: a = 4 + 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 E222: a = 4 + 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 E223: a = 4\t+ 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 E224: a = 4 +\t5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
622 for match in OPERATOR_REGEX.finditer(logical_line):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
623 before, after = match.groups()
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
625 if '\t' in before:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
626 yield match.start(1), "E223 tab before operator"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
627 elif len(before) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
628 yield match.start(1), "E221 multiple spaces before operator"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
629
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
630 if '\t' in after:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
631 yield match.start(2), "E224 tab after operator"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
632 elif len(after) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
633 yield match.start(2), "E222 multiple spaces after operator"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634
3209
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
635
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 def missing_whitespace_around_operator(logical_line, tokens):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
637 r"""Surround operators with a single space on either side.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
638
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 - Always surround these binary operators with a single space on
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 either side: assignment (=), augmented assignment (+=, -= etc.),
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
641 comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 Booleans (and, or, not).
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
644 - If operators with different priorities are used, consider adding
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
645 whitespace around the operators with the lowest priorities.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 Okay: i = i + 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 Okay: submitted += 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 Okay: x = x * 2 - 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 Okay: hypot2 = x * x + y * y
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 Okay: c = (a + b) * (a - b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 Okay: foo(bar, key='word', *args, **kwargs)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 Okay: alpha[:-i]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 E225: i=i+1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 E225: submitted +=1
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
657 E225: x = x /2 - 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 E225: z = x **y
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
659 E226: c = (a+b) * (a-b)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
660 E226: hypot2 = x*x + y*y
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
661 E227: c = a|b
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
662 E228: msg = fmt%(errno, errmsg)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 parens = 0
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 need_space = False
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 prev_type = tokenize.OP
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 prev_text = prev_end = None
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 for token_type, text, start, end, line in tokens:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
669 if token_type in SKIP_COMMENTS:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 continue
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 if text in ('(', 'lambda'):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 parens += 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 elif text == ')':
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 parens -= 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 if need_space:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 if start != prev_end:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
677 # Found a (probably) needed space
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
678 if need_space is not True and not need_space[1]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
679 yield (need_space[0],
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
680 "E225 missing whitespace around operator")
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 need_space = False
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
682 elif text == '>' and prev_text in ('<', '-'):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 # Tolerate the "<>" operator, even if running Python 3
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
684 # Deal with Python 3's annotated return value "->"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 else:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
687 if need_space is True or need_space[1]:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
688 # A needed trailing space was not found
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
689 yield prev_end, "E225 missing whitespace around operator"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
690 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
691 code, optype = 'E226', 'arithmetic'
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
692 if prev_text == '%':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
693 code, optype = 'E228', 'modulo'
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
694 elif prev_text not in ARITHMETIC_OP:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
695 code, optype = 'E227', 'bitwise or shift'
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
696 yield (need_space[0], "%s missing whitespace "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
697 "around %s operator" % (code, optype))
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
698 need_space = False
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 elif token_type == tokenize.OP and prev_end is not None:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700 if text == '=' and parens:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 # Allow keyword args or defaults: foo(bar=None).
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702 pass
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
703 elif text in WS_NEEDED_OPERATORS:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 need_space = True
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 elif text in UNARY_OPERATORS:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
706 # Check if the operator is being used as a binary operator
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 # Allow unary operators: -123, -x, +1.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708 # Allow argument unpacking: foo(*args, **kwargs).
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
709 if (prev_text in '}])' if prev_type == tokenize.OP
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
710 else prev_text not in KEYWORDS):
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
711 need_space = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
712 elif text in WS_OPTIONAL_OPERATORS:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
713 need_space = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
714
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
715 if need_space is None:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
716 # Surrounding space is optional, but ensure that
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
717 # trailing space matches opening space
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
718 need_space = (prev_end, start != prev_end)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
719 elif need_space and start == prev_end:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
720 # A needed opening space was not found
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
721 yield prev_end, "E225 missing whitespace around operator"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
722 need_space = False
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 prev_type = token_type
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 prev_text = text
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 def whitespace_around_comma(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
729 r"""Avoid extraneous whitespace after a comma or a colon.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 Note: these checks are disabled by default
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 Okay: a = (1, 2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 E241: a = (1, 2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 E242: a = (1,\t2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 line = logical_line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
738 for m in WHITESPACE_AFTER_COMMA_REGEX.finditer(line):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
739 found = m.start() + 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
740 if '\t' in m.group():
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
741 yield found, "E242 tab after '%s'", m.group()[0]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
742 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
743 yield found, "E241 multiple spaces after '%s'", m.group()[0]
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
746 def whitespace_around_named_parameter_equals(logical_line, tokens):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
747 r"""Don't use spaces around the '=' sign in function arguments.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
748
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 Don't use spaces around the '=' sign when used to indicate a
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 keyword argument or a default parameter value.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 Okay: def complex(real, imag=0.0):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 Okay: return magic(r=real, i=imag)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 Okay: boolean(a == b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 Okay: boolean(a != b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 Okay: boolean(a <= b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 Okay: boolean(a >= b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 E251: def complex(real, imag = 0.0):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 E251: return magic(r = real, i = imag)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
761 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762 parens = 0
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
763 no_space = False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
764 prev_end = None
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
765 message = "E251 unexpected spaces around keyword / parameter equals"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
766 for token_type, text, start, end, line in tokens:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
767 if token_type == tokenize.NL:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
768 continue
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
769 if no_space:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
770 no_space = False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
771 if start != prev_end:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
772 yield (prev_end, message)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
773 elif token_type == tokenize.OP:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
774 if text == '(':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
775 parens += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
776 elif text == ')':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
777 parens -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
778 elif parens and text == '=':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
779 no_space = True
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
780 if start != prev_end:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
781 yield (prev_end, message)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
782 prev_end = end
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
785 def whitespace_before_comment(logical_line, tokens):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
786 r"""Separate inline comments by at least two spaces.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 An inline comment is a comment on the same line as a statement. Inline
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 comments should be separated by at least two spaces from the statement.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 They should start with a # and a single space.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
792 Each line of a block comment starts with a # and a single space
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
793 (unless it is indented text inside the comment).
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
794
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 Okay: x = x + 1 # Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 Okay: x = x + 1 # Increment x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
797 Okay: # Block comment
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 E261: x = x + 1 # Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 E262: x = x + 1 #Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 E262: x = x + 1 # Increment x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
801 E265: #Block comment
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 prev_end = (0, 0)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 for token_type, text, start, end, line in tokens:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 if token_type == tokenize.COMMENT:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
806 inline_comment = line[:start[1]].strip()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
807 if inline_comment:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
808 if prev_end[0] == start[0] and start[1] < prev_end[1] + 2:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
809 yield (prev_end,
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
810 "E261 at least two spaces before inline comment")
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
811 symbol, sp, comment = text.partition(' ')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
812 bad_prefix = symbol not in ('#', '#:')
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
813 if inline_comment:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
814 if bad_prefix or comment[:1].isspace():
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
815 yield start, "E262 inline comment should start with '# '"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
816 elif bad_prefix:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
817 if text.rstrip('#') and (start[0] > 1 or symbol[1] != '!'):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
818 yield start, "E265 block comment should start with '# '"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
819 elif token_type != tokenize.NL:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 def imports_on_separate_lines(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
824 r"""Imports should usually be on separate lines.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 Okay: import os\nimport sys
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827 E401: import sys, os
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829 Okay: from subprocess import Popen, PIPE
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 Okay: from myclas import MyClass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831 Okay: from foo.bar.yourclass import YourClass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 Okay: import myclass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 Okay: import foo.bar.yourclass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 if line.startswith('import '):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 found = line.find(',')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
838 if -1 < found and ';' not in line[:found]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
839 yield found, "E401 multiple imports on one line"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 def compound_statements(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
843 r"""Compound statements (on the same line) are generally discouraged.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845 While sometimes it's okay to put an if/for/while with a small body
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
846 on the same line, never do this for multi-clause statements.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
847 Also avoid folding such long lines!
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
849 Okay: if foo == 'blah':\n do_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 Okay: do_one()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851 Okay: do_two()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 Okay: do_three()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 E701: if foo == 'blah': do_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 E701: for x in lst: total += x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 E701: while t < 10: t = delay()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857 E701: if foo == 'blah': do_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
858 E701: else: do_non_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 E701: try: something()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 E701: finally: cleanup()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 E701: if foo == 'blah': one(); two(); three()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 E702: do_one(); do_two(); do_three()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
864 E703: do_four(); # useless semicolon
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866 line = logical_line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
867 last_char = len(line) - 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
868 found = line.find(':')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
869 while -1 < found < last_char:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
870 before = line[:found]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871 if (before.count('{') <= before.count('}') and # {'a': 1} (dict)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
872 before.count('[') <= before.count(']') and # [1:2] (slice)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
873 before.count('(') <= before.count(')') and # (Python 3 annotation)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
874 not LAMBDA_REGEX.search(before)): # lambda x: x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
875 yield found, "E701 multiple statements on one line (colon)"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
876 found = line.find(':', found + 1)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
877 found = line.find(';')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
878 while -1 < found:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
879 if found < last_char:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
880 yield found, "E702 multiple statements on one line (semicolon)"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
881 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
882 yield found, "E703 statement ends with a semicolon"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
883 found = line.find(';', found + 1)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
884
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
885
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
886 def explicit_line_join(logical_line, tokens):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
887 r"""Avoid explicit line join between brackets.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
888
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
889 The preferred way of wrapping long lines is by using Python's implied line
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
890 continuation inside parentheses, brackets and braces. Long lines can be
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
891 broken over multiple lines by wrapping expressions in parentheses. These
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
892 should be used in preference to using a backslash for line continuation.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
893
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
894 E502: aaa = [123, \\n 123]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
895 E502: aaa = ("bbb " \\n "ccc")
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
896
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
897 Okay: aaa = [123,\n 123]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
898 Okay: aaa = ("bbb "\n "ccc")
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
899 Okay: aaa = "bbb " \\n "ccc"
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
900 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
901 prev_start = prev_end = parens = 0
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
902 backslash = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
903 for token_type, text, start, end, line in tokens:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
904 if start[0] != prev_start and parens and backslash:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
905 yield backslash, "E502 the backslash is redundant between brackets"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
906 if end[0] != prev_end:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
907 if line.rstrip('\r\n').endswith('\\'):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
908 backslash = (end[0], len(line.splitlines()[-1]) - 1)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
909 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
910 backslash = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
911 prev_start = prev_end = end[0]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
912 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
913 prev_start = start[0]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
914 if token_type == tokenize.OP:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
915 if text in '([{':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
916 parens += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
917 elif text in ')]}':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
918 parens -= 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
920
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
921 def comparison_to_singleton(logical_line, noqa):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
922 r"""Comparison to singletons should use "is" or "is not".
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
923
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
924 Comparisons to singletons like None should always be done
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
925 with "is" or "is not", never the equality operators.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
926
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
927 Okay: if arg is not None:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
928 E711: if arg != None:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
929 E712: if arg == True:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
930
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
931 Also, beware of writing if x when you really mean if x is not None --
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
932 e.g. when testing whether a variable or argument that defaults to None was
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
933 set to some other value. The other value might have a type (such as a
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
934 container) that could be false in a boolean context!
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
936 match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
937 if match:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
938 same = (match.group(1) == '==')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
939 singleton = match.group(2)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
940 msg = "'if cond is %s:'" % (('' if same else 'not ') + singleton)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
941 if singleton in ('None',):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
942 code = 'E711'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
943 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
944 code = 'E712'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
945 nonzero = ((singleton == 'True' and same) or
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
946 (singleton == 'False' and not same))
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
947 msg += " or 'if %scond:'" % ('' if nonzero else 'not ')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
948 yield (match.start(1), "%s comparison to %s should be %s" %
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
949 (code, singleton, msg), singleton, msg)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
950
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
951
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
952 def comparison_negative(logical_line):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
953 r"""Negative comparison should be done using "not in" and "is not".
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
954
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
955 Okay: if x not in y:\n pass
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
956 Okay: assert (X in Y or X is Z)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
957 Okay: if not (X in Y):\n pass
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
958 Okay: zz = x is not y
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
959 E713: Z = not X in Y
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
960 E713: if not X.B in Y:\n pass
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
961 E714: if not X is Y:\n pass
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
962 E714: Z = not X.B is Y
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
963 """
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
964 match = COMPARE_NEGATIVE_REGEX.search(logical_line)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
965 if match:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
966 pos = match.start(1)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
967 if match.group(2) == 'in':
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
968 yield pos, "E713 test for membership should be 'not in'"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
969 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
970 yield pos, "E714 test for object identity should be 'is not'"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
971
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
972
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
973 def comparison_type(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
974 r"""Object type comparisons should always use isinstance().
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
975
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
976 Do not compare types directly.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
977
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
978 Okay: if isinstance(obj, int):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
979 E721: if type(obj) is type(1):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
980
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
981 When checking if an object is a string, keep in mind that it might be a
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
982 unicode string too! In Python 2.3, str and unicode have a common base
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
983 class, basestring, so you can do:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
984
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
985 Okay: if isinstance(obj, basestring):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
986 Okay: if type(a1) is type(b1):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
987 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
988 match = COMPARE_TYPE_REGEX.search(logical_line)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
989 if match:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
990 inst = match.group(1)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
991 if inst and isidentifier(inst) and inst not in SINGLETONS:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
992 return # Allow comparison for types which are not obvious
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
993 yield match.start(), "E721 do not compare types, use 'isinstance()'"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
994
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
995
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
996 def python_3000_has_key(logical_line, noqa):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
997 r"""The {}.has_key() method is removed in Python 3: use the 'in' operator.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
998
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
999 Okay: if "alph" in d:\n print d["alph"]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1000 W601: assert d.has_key('alph')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1001 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1002 pos = logical_line.find('.has_key(')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1003 if pos > -1 and not noqa:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1004 yield pos, "W601 .has_key() is deprecated, use 'in'"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1005
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1006
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1007 def python_3000_raise_comma(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1008 r"""When raising an exception, use "raise ValueError('message')".
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1009
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1010 The older form is removed in Python 3.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1011
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1012 Okay: raise DummyError("Message")
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1013 W602: raise DummyError, "Message"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1015 match = RAISE_COMMA_REGEX.match(logical_line)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1016 if match and not RERAISE_COMMA_REGEX.match(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1017 yield match.end() - 1, "W602 deprecated form of raising exception"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1018
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1019
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1020 def python_3000_not_equal(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1021 r"""New code should always use != instead of <>.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1022
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1023 The older syntax is removed in Python 3.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1024
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1025 Okay: if a != 'no':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1026 W603: if a <> 'no':
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1027 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1028 pos = logical_line.find('<>')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1029 if pos > -1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1030 yield pos, "W603 '<>' is deprecated, use '!='"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1031
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1032
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1033 def python_3000_backticks(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1034 r"""Backticks are removed in Python 3: use repr() instead.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1035
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1036 Okay: val = repr(1 + 2)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1037 W604: val = `1 + 2`
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1038 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1039 pos = logical_line.find('`')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1040 if pos > -1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1041 yield pos, "W604 backticks are deprecated, use 'repr()'"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1045 # Helper functions
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1047
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 if '' == ''.encode():
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1050 # Python 2: implicit encoding.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1051 def readlines(filename):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1052 """Read the source code."""
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1053 with open(filename) as f:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1054 return f.readlines()
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1055 isidentifier = re.compile(r'[a-zA-Z_]\w*').match
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1056 stdin_get_value = sys.stdin.read
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1057 else:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1058 # Python 3
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1059 def readlines(filename):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1060 """Read the source code."""
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1061 try:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1062 with open(filename, 'rb') as f:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1063 (coding, lines) = tokenize.detect_encoding(f.readline)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1064 f = TextIOWrapper(f, coding, line_buffering=True)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1065 return [l.decode(coding) for l in lines] + f.readlines()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1066 except (LookupError, SyntaxError, UnicodeError):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1067 # Fall back if file encoding is improperly declared
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1068 with open(filename, encoding='latin-1') as f:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1069 return f.readlines()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1070 isidentifier = str.isidentifier
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1071
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1072 def stdin_get_value():
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1073 return TextIOWrapper(sys.stdin.buffer, errors='ignore').read()
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1074 noqa = re.compile(r'# no(?:qa|pep8)\b', re.I).search
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1075
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1076
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1077 def expand_indent(line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1078 r"""Return the amount of indentation.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1079
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1080 Tabs are expanded to the next multiple of 8.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1081
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1082 >>> expand_indent(' ')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1083 4
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1084 >>> expand_indent('\t')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: