Plugins/CheckerPlugins/CodeStyleChecker/pep8.py

Wed, 17 Feb 2016 22:11:12 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Wed, 17 Feb 2016 22:11:12 +0100
changeset 4828
b313794f46a2
parent 4631
5c1a96925da4
child 5146
f96c32abd120
permissions
-rw-r--r--

updated pep8 to 1.7.0

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>
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
7 # Copyright (C) 2014-2016 Ian Lee <ianlee1521@gmail.com>
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # 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
10 # 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
11 # (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
12 # 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
13 # 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
14 # 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
15 # subject to the following conditions:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 # 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
18 # 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
19 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 # 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
21 # 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
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 # 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
24 # 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
25 # 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
26 # 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
27 # SOFTWARE.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
29 r"""
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
30 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
31
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 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
33 $ python pep8.py -h
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 This program and its regression test suite live here:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
36 https://github.com/pycqa/pep8
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 Groups of errors and warnings:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 E errors
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 W warnings
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 100 indentation
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 200 whitespace
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 300 blank lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 400 imports
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 500 line length
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 600 deprecation
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 700 statements
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
48 900 syntax error
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 """
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
50 from __future__ import with_statement
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 #
2861
cdcbca0cea82 Fixed a typo in the included pep8.py modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2799
diff changeset
53 # This is a modified version to make the original pep8.py better suitable
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3629
diff changeset
54 # for being called from within the eric6 IDE. The modifications are as
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # follows:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 # - made messages translatable via Qt
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3629
diff changeset
58 # - added code for eric6 integration
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 #
4631
5c1a96925da4 Updated copyright for 2016.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
60 # Copyright (c) 2011 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 import os
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 import sys
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 import re
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 import time
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 import inspect
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 import keyword
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 import tokenize
3210
8f4fe6f76729 Fixed some dubious code related to AST generation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
70 import ast
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 from optparse import OptionParser
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 from fnmatch import fnmatch
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 try:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
74 from configparser import RawConfigParser
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
75 from io import TextIOWrapper
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
76 except ImportError:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
77 from ConfigParser import RawConfigParser # __IGNORE_WARNING__
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
79 __version__ = '1.7.0'
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
80
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
81 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox'
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
82 DEFAULT_IGNORE = 'E121,E123,E126,E226,E24,E704'
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
83 try:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
84 if sys.platform == 'win32':
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
85 USER_CONFIG = os.path.expanduser(r'~\.pep8')
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
86 else:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
87 USER_CONFIG = os.path.join(
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
88 os.getenv('XDG_CONFIG_HOME') or os.path.expanduser('~/.config'),
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
89 'pep8'
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
90 )
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
91 except ImportError:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
92 USER_CONFIG = None
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
93
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
94 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
95 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
96 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
97 REPORT_FORMAT = {
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
98 '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
99 '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
100 }
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 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
103 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
104 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
105 ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
106 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
107 WS_NEEDED_OPERATORS = frozenset([
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
108 '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
109 '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
110 WHITESPACE = frozenset(' \t')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
111 NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
112 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
113 # 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
114 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
115 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
116
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 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
118 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
119 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
120 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
121 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 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
123 WHITESPACE_AFTER_COMMA_REGEX = re.compile(r'[,;:]\s*(?: |\t)')
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
124 COMPARE_SINGLETON_REGEX = re.compile(r'(\bNone|\bFalse|\bTrue)?\s*([=!]=)'
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
125 r'\s*(?(1)|(None|False|True))\b')
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
126 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
127 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
128 r'|\s*\(\s*([^)]*[^ )])\s*\))')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
129 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
130 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
131 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
132 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
133
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
134 # 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
135 # 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
136 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
137
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 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 # Plugins (check functions) for physical lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 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
145 r"""Never mix tabs and spaces.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 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
148 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
149 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
150 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
151 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
152 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
153
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 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
155 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
156 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 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
158 for offset, char in enumerate(indent):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 if char != indent_char:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
160 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
161
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 def tabs_obsolete(physical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
164 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
165
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 Okay: if True:\n return
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 W191: if True:\n\treturn
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 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
170 if '\t' in indent:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
171 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
172
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 def trailing_whitespace(physical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
175 r"""Trailing whitespace is superfluous.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 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
178 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
179
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
180 Okay: spam(1)\n#
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
181 W291: spam(1) \n#
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 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
183 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 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
185 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
186 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
187 stripped = physical_line.rstrip(' \t\v')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 if physical_line != stripped:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 if stripped:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
190 return len(stripped), "W291 trailing whitespace"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
192 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
193
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 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
196 r"""Trailing blank lines are superfluous.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 Okay: spam(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 W391: spam(1)\n
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
200
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
201 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
202 """
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
203 if line_number == total_lines:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
204 stripped_last_line = physical_line.rstrip()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
205 if not stripped_last_line:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
206 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
207 if stripped_last_line == physical_line:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
208 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
209
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
211 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
212 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
213
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 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
215 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
216 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
217 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
218 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
219 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
220
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
221 Reports error E501.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 line = physical_line.rstrip()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 length = len(line)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
225 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
226 # 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
227 # 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
228 chunks = line.split()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
229 if ((len(chunks) == 1 and multiline) or
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
230 (len(chunks) == 2 and chunks[0] == '#')) and \
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
231 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
232 return
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
233 if hasattr(line, 'decode'): # Python 2
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 # 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
235 try:
1145
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
236 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
237 except UnicodeError:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
238 pass
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
239 if length > max_line_length:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
240 return (max_line_length, "E501 line too long "
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
241 "(%d > %d characters)" % (length, max_line_length),
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
242 length, max_line_length)
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
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 # Plugins (check functions) for logical lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 ##############################################################################
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248
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 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
251 blank_before, previous_logical, previous_indent_level):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
252 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
253
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 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
255
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 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
257 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
258 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
259
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 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
261
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 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
263 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
264
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 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
266 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
267 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
268 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
269 E304: @decorator\n\ndef a():\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
271 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
272 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
273 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
274 if blank_lines:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
275 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
276 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
277 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
278 elif logical_line.startswith(('def ', 'class ', '@')):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 if indent_level:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
280 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
281 DOCSTRING_REGEX.match(previous_logical)):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
282 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
283 elif blank_before != 2:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
284 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
285
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 def extraneous_whitespace(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
288 r"""Avoid extraneous whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
290 Avoid extraneous whitespace in these situations:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 - Immediately inside parentheses, brackets or braces.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 - Immediately before a comma, semicolon, or colon.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 Okay: spam(ham[1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 E201: spam( ham[1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 E201: spam(ham[ 1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 E201: spam(ham[1], { eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 E202: spam(ham[1], {eggs: 2} )
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 E202: spam(ham[1 ], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 E202: spam(ham[1], {eggs: 2 })
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 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
303 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
304 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
305 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 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
308 text = match.group()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 char = text.strip()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 found = match.start()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
311 if text == char + ' ':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
312 # assert char in '([{'
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
313 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
314 elif line[found - 1] != ',':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
315 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
316 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
317
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
318
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
319 def whitespace_around_keywords(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
320 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
321
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
322 Okay: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
323 E271: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
324 E272: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
325 E273: True and\tFalse
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
326 E274: True\tand False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
327 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
328 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
329 before, after = match.groups()
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
330
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
331 if '\t' in before:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
332 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
333 elif len(before) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
334 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
335
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
336 if '\t' in after:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
337 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
338 elif len(after) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
339 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
340
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 def missing_whitespace(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
343 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
344
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 Okay: [a, b]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 Okay: (3,)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 Okay: a[1:4]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 Okay: a[:4]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 Okay: a[1:]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 Okay: a[1:4:2]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 E231: ['a','b']
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 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
353 E231: [{'a':'b'}]
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 for index in range(len(line) - 1):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 char = line[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 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
359 before = line[:index]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
360 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
361 before.rfind('{') < before.rfind('['):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 continue # Slice syntax, no space required
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 if char == ',' and line[index + 1] == ')':
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 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
365 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
366
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 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
369 indent_level, previous_indent_level):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
370 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
371
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 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
373 use 8-space tabs.
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 Okay: a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 Okay: if a == 0:\n a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 E111: a = 1
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
378 E114: # a = 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 Okay: for item in items:\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 E112: for item in items:\npass
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
382 E115: for item in items:\n# Hi\n pass
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 Okay: a = 1\nb = 2
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 E113: a = 1\n b = 2
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
386 E116: a = 1\n # b = 2
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 """
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
388 c = 0 if logical_line else 3
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
389 tmpl = "E11%d %s" if logical_line else "E11%d %s (comment)"
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
390 if indent_level % 4:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
391 yield 0, tmpl % (1 + c, "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
392 indent_expect = previous_logical.endswith(':')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 if indent_expect and indent_level <= previous_indent_level:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
394 yield 0, tmpl % (2 + c, "expected an indented block")
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
395 elif not indent_expect and indent_level > previous_indent_level:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
396 yield 0, tmpl % (3 + c, "unexpected indentation")
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
397
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
398
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
399 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
400 indent_char, noqa, verbose):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
401 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
402
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
403 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
404 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
405 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
406
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
407 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
408 - 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
409 - 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
410 continuation line.
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 Okay: a = (\n)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
413 E123: a = (\n )
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
414
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
415 Okay: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
416 E121: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
417 E122: a = (\n42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
418 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
419 E124: a = (24,\n 42\n)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
420 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
421 E126: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
422 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
423 E128: a = (24,\n 42)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
424 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
425 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
426 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
427 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
428 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
429 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
430 return
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
431
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
432 # 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
433 # 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
434 # 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
435 # 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
436 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
437
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
438 row = depth = 0
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
439 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
440 # 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
441 parens = [0] * nrows
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
442 # 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
443 rel_indent = [0] * nrows
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
444 # 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
445 open_rows = [[0]]
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
446 # for each depth, memorize the hanging indentation
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
447 hangs = [None]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
448 # visual indents
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
449 indent_chances = {}
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
450 last_indent = tokens[0][2]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
451 visual_indent = None
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
452 last_token_multiline = False
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
453 # 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
454 indent = [last_indent[1]]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
455 if verbose >= 3:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
456 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
457
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
458 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
459
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
460 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
461 if newline:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
462 row = start[0] - first_row
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
463 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
464
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
465 if newline:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
466 # 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
467 last_indent = start
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
468 if verbose >= 3:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
469 print("... " + line.rstrip())
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
470
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
471 # record the initial indent.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
472 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
473
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
474 # identify closing bracket
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
475 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
476
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
477 # 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
478 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
479 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
480 hanging_indent = hang in valid_hangs
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
481 if hanging_indent:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
482 break
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
483 if hangs[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
484 hanging_indent = (hang == hangs[depth])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
485 # 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
486 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
487 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
488
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
489 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
490 # 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
491 if start[1] != indent[depth]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
492 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
493 "visual indentation")
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
494 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
495 # 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
496 if hang_closing:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
497 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
498 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
499 if visual_indent is not True:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
500 # visual indent is broken
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
501 yield (start, "E128 continuation line "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
502 "under-indented for visual indent")
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
503 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
504 # hanging indent is verified
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
505 if close_bracket and not hang_closing:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
506 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
507 "indentation of opening bracket's line")
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
508 hangs[depth] = hang
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
509 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
510 # visual indent is verified
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
511 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
512 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
513 # 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
514 pass
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
515 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
516 # indent is broken
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
517 if hang <= 0:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
518 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
519 elif indent[depth]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
520 error = "E127", "over-indented for visual indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
521 elif not close_bracket and hangs[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
522 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
523 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
524 hangs[depth] = hang
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
525 if hang > 4:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
526 error = "E126", "over-indented for hanging indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
527 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
528 error = "E121", "under-indented for hanging indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
529 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
530
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
531 # look for visual indenting
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
532 if (parens[row] and
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
533 token_type not in (tokenize.NL, tokenize.COMMENT) and
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
534 not indent[depth]):
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
535 indent[depth] = start[1]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
536 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
537 if verbose >= 4:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
538 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
539 # 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
540 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
541 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
542 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
543 # 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
544 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
545 indent_chances[end[1] + 1] = True
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
546 elif text == ':' and line[end[1]:].isspace():
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
547 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
548
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
549 # 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
550 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
551 if text in '([{':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
552 depth += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
553 indent.append(0)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
554 hangs.append(None)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
555 if len(open_rows) == depth:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
556 open_rows.append([])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
557 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
558 parens[row] += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
559 if verbose >= 4:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
560 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
561 (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
562 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
563 # 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
564 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
565 hangs.pop()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
566 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
567 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
568 indent[d] = 0
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
569 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
570 if ind >= prev_indent:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
571 del indent_chances[ind]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
572 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
573 depth -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
574 if depth:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
575 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
576 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
577 if parens[idx]:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
578 parens[idx] -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
579 break
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
580 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
581 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
582 # 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
583 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
584
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
585 last_token_multiline = (start[0] != end[0])
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
586 if last_token_multiline:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
587 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
588
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
589 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
590 pos = (start[0], indent[0] + 4)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
591 if visual_indent:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
592 code = "E129 visually indented line"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
593 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
594 code = "E125 continuation line"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
595 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
596
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 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
599 r"""Avoid extraneous whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
601 Avoid extraneous whitespace in the following situations:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
602 - 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
603 function call.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
604 - 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
605
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606 Okay: spam(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 E211: spam (1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 Okay: dict['key'] = list[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 E211: dict ['key'] = list[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 E211: dict['key'] = list [index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
613 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
614 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
615 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
616 if (token_type == tokenize.OP and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 text in '([' and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 start != prev_end and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 (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
620 # 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
621 (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
622 # 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
623 not keyword.iskeyword(prev_text)):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
624 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
625 prev_type = token_type
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 prev_text = text
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 def whitespace_around_operator(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
631 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
632
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 Okay: a = 12 + 3
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 E221: a = 4 + 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 E222: a = 4 + 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 E223: a = 4\t+ 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 E224: a = 4 +\t5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
639 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
640 before, after = match.groups()
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
642 if '\t' in before:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
643 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
644 elif len(before) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
645 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
646
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
647 if '\t' in after:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
648 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
649 elif len(after) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
650 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
651
3209
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
652
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 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
654 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
655
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
656 - 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
657 either side: assignment (=), augmented assignment (+=, -= etc.),
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
658 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
659 Booleans (and, or, not).
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
661 - 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
662 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
663
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 Okay: i = i + 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 Okay: submitted += 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 Okay: x = x * 2 - 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 Okay: hypot2 = x * x + y * y
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 Okay: c = (a + b) * (a - b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 Okay: foo(bar, key='word', *args, **kwargs)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 Okay: alpha[:-i]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 E225: i=i+1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 E225: submitted +=1
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
674 E225: x = x /2 - 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 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
676 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
677 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
678 E227: c = a|b
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
679 E228: msg = fmt%(errno, errmsg)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 parens = 0
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 need_space = False
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 prev_type = tokenize.OP
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 prev_text = prev_end = None
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 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
686 if token_type in SKIP_COMMENTS:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 continue
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 if text in ('(', 'lambda'):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 parens += 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 elif text == ')':
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691 parens -= 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 if need_space:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 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
694 # 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
695 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
696 yield (need_space[0],
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
697 "E225 missing whitespace around operator")
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 need_space = False
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
699 elif text == '>' and prev_text in ('<', '-'):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700 # 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
701 # 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
702 pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 else:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
704 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
705 # 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
706 yield prev_end, "E225 missing whitespace around operator"
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
707 elif prev_text != '**':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
708 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
709 if prev_text == '%':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
710 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
711 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
712 code, optype = 'E227', 'bitwise or shift'
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
713 yield (need_space[0], "%s missing whitespace "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
714 "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
715 need_space = False
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 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
717 if text == '=' and parens:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 # 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
719 pass
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
720 elif text in WS_NEEDED_OPERATORS:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 need_space = True
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 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
723 # 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
724 # Allow unary operators: -123, -x, +1.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 # Allow argument unpacking: foo(*args, **kwargs).
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
726 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
727 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
728 need_space = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
729 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
730 need_space = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
731
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
732 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
733 # 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
734 # 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
735 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
736 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
737 # 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
738 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
739 need_space = False
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 prev_type = token_type
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 prev_text = text
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743
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 def whitespace_around_comma(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
746 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
747
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 Note: these checks are disabled by default
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 Okay: a = (1, 2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 E241: a = (1, 2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 E242: a = (1,\t2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 line = logical_line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
755 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
756 found = m.start() + 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
757 if '\t' in m.group():
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
758 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
759 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
760 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
761
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
763 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
764 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
765
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 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
767 keyword argument or a default parameter value.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 Okay: def complex(real, imag=0.0):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 Okay: return magic(r=real, i=imag)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 Okay: boolean(a == b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 Okay: boolean(a != b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 Okay: boolean(a <= b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 Okay: boolean(a >= b)
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
775 Okay: def foo(arg: int = 42):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 E251: def complex(real, imag = 0.0):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 E251: return magic(r = real, i = imag)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 parens = 0
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
781 no_space = False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
782 prev_end = None
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
783 annotated_func_arg = False
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
784 in_def = logical_line.startswith('def')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
785 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
786 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
787 if token_type == tokenize.NL:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
788 continue
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
789 if no_space:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
790 no_space = False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
791 if start != prev_end:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
792 yield (prev_end, message)
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
793 if token_type == tokenize.OP:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
794 if text == '(':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
795 parens += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
796 elif text == ')':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
797 parens -= 1
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
798 elif in_def and text == ':' and parens == 1:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
799 annotated_func_arg = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
800 elif parens and text == ',' and parens == 1:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
801 annotated_func_arg = False
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
802 elif parens and text == '=' and not annotated_func_arg:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
803 no_space = True
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
804 if start != prev_end:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
805 yield (prev_end, message)
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
806 if not parens:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
807 annotated_func_arg = False
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
808
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
809 prev_end = end
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
812 def whitespace_before_comment(logical_line, tokens):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
813 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
814
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815 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
816 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
817 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
818
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
819 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
820 (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
821
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822 Okay: x = x + 1 # Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 Okay: x = x + 1 # Increment x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
824 Okay: # Block comment
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825 E261: x = x + 1 # Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 E262: x = x + 1 #Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827 E262: x = x + 1 # Increment x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
828 E265: #Block comment
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
829 E266: ### Block comment
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831 prev_end = (0, 0)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 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
833 if token_type == tokenize.COMMENT:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
834 inline_comment = line[:start[1]].strip()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
835 if inline_comment:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
836 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
837 yield (prev_end,
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
838 "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
839 symbol, sp, comment = text.partition(' ')
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
840 bad_prefix = symbol not in '#:' and (symbol.lstrip('#')[:1] or '#')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
841 if inline_comment:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
842 if bad_prefix or comment[:1] in WHITESPACE:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
843 yield start, "E262 inline comment should start with '# '"
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
844 elif bad_prefix and (bad_prefix != '!' or start[0] > 1):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
845 if bad_prefix != '#':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
846 yield start, "E265 block comment should start with '# '"
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
847 elif comment:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
848 yield start, "E266 too many leading '#' for block comment"
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
849 elif token_type != tokenize.NL:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853 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
854 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
855
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 Okay: import os\nimport sys
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857 E401: import sys, os
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
858
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 Okay: from subprocess import Popen, PIPE
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 Okay: from myclas import MyClass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 Okay: from foo.bar.yourclass import YourClass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862 Okay: import myclass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 Okay: import foo.bar.yourclass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866 if line.startswith('import '):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
867 found = line.find(',')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
868 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
869 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
870
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
872 def module_imports_on_top_of_file(
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
873 logical_line, indent_level, checker_state, noqa):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
874 r"""Imports are always put at the top of the file, just after any module
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
875 comments and docstrings, and before module globals and constants.
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
876
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
877 Okay: import os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
878 Okay: # this is a comment\nimport os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
879 Okay: '''this is a module docstring'''\nimport os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
880 Okay: r'''this is a module docstring'''\nimport os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
881 Okay: try:\n import x\nexcept:\n pass\nelse:\n pass\nimport y
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
882 Okay: try:\n import x\nexcept:\n pass\nfinally:\n pass\nimport y
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
883 E402: a=1\nimport os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
884 E402: 'One string'\n"Two string"\nimport os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
885 E402: a=1\nfrom sys import x
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
886
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
887 Okay: if x:\n import os
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
888 """
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
889 def is_string_literal(line):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
890 if line[0] in 'uUbB':
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
891 line = line[1:]
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
892 if line and line[0] in 'rR':
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
893 line = line[1:]
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
894 return line and (line[0] == '"' or line[0] == "'")
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
895
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
896 allowed_try_keywords = ('try', 'except', 'else', 'finally')
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
897
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
898 if indent_level: # Allow imports in conditional statements or functions
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
899 return
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
900 if not logical_line: # Allow empty lines or comments
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
901 return
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
902 if noqa:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
903 return
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
904 line = logical_line
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
905 if line.startswith('import ') or line.startswith('from '):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
906 if checker_state.get('seen_non_imports', False):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
907 yield 0, "E402 module level import not at top of file"
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
908 elif any(line.startswith(kw) for kw in allowed_try_keywords):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
909 # Allow try, except, else, finally keywords intermixed with imports in
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
910 # order to support conditional importing
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
911 return
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
912 elif is_string_literal(line):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
913 # The first literal is a docstring, allow it. Otherwise, report error.
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
914 if checker_state.get('seen_docstring', False):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
915 checker_state['seen_non_imports'] = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
916 else:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
917 checker_state['seen_docstring'] = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
918 else:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
919 checker_state['seen_non_imports'] = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
920
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
921
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 def compound_statements(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
923 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
924
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 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
926 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
927 Also avoid folding such long lines!
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
929 Always use a def statement instead of an assignment statement that
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
930 binds a lambda expression directly to a name.
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
931
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 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
933 Okay: do_one()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 Okay: do_two()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 Okay: do_three()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 E701: if foo == 'blah': do_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 E701: for x in lst: total += x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 E701: while t < 10: t = delay()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 E701: if foo == 'blah': do_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 E701: else: do_non_blah_thing()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942 E701: try: something()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
943 E701: finally: cleanup()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944 E701: if foo == 'blah': one(); two(); three()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 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
946 E703: do_four(); # useless semicolon
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
947 E704: def f(x): return 2*x
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
948 E731: f = lambda x: 2*x
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 line = logical_line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
951 last_char = len(line) - 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
952 found = line.find(':')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
953 while -1 < found < last_char:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 before = line[:found]
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
955 if ((before.count('{') <= before.count('}') and # {'a': 1} (dict)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
956 before.count('[') <= before.count(']') and # [1:2] (slice)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
957 before.count('(') <= before.count(')'))): # (annotation)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
958 lambda_kw = LAMBDA_REGEX.search(before)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
959 if lambda_kw:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
960 before = line[:lambda_kw.start()].rstrip()
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
961 if before[-1:] == '=' and isidentifier(before[:-1].strip()):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
962 yield 0, ("E731 do not assign a lambda expression, use a "
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
963 "def")
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
964 break
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
965 if before.startswith('def '):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
966 yield 0, "E704 multiple statements on one line (def)"
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
967 else:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
968 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
969 found = line.find(':', found + 1)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
970 found = line.find(';')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
971 while -1 < found:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
972 if found < last_char:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
973 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
974 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
975 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
976 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
977
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
978
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
979 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
980 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
981
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
982 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
983 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
984 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
985 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
986
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
987 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
988 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
989
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
990 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
991 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
992 Okay: aaa = "bbb " \\n "ccc"
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
993 Okay: aaa = 123 # \\
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 prev_start = prev_end = parens = 0
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
996 comment = False
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
997 backslash = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
998 for token_type, text, start, end, line in tokens:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
999 if token_type == tokenize.COMMENT:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1000 comment = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1001 if start[0] != prev_start and parens and backslash and not comment:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1002 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
1003 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
1004 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
1005 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
1006 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1007 backslash = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1008 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
1009 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1010 prev_start = start[0]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1011 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
1012 if text in '([{':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1013 parens += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1014 elif text in ')]}':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1015 parens -= 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1016
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1017
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1018 def break_around_binary_operator(logical_line, tokens):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1019 r"""
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1020 Avoid breaks before binary operators.
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1021
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1022 The preferred place to break around a binary operator is after the
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1023 operator, not before it.
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1024
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1025 W503: (width == 0\n + height == 0)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1026 W503: (width == 0\n and height == 0)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1027
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1028 Okay: (width == 0 +\n height == 0)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1029 Okay: foo(\n -x)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1030 Okay: foo(x\n [])
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1031 Okay: x = '''\n''' + ''
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1032 Okay: foo(x,\n -y)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1033 Okay: foo(x, # comment\n -y)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1034 """
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1035 def is_binary_operator(token_type, text):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1036 # The % character is strictly speaking a binary operator, but the
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1037 # common usage seems to be to put it next to the format parameters,
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1038 # after a line break.
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1039 return ((token_type == tokenize.OP or text in ['and', 'or']) and
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1040 text not in "()[]{},:.;@=%")
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1041
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1042 line_break = False
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1043 unary_context = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1044 for token_type, text, start, end, line in tokens:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1045 if token_type == tokenize.COMMENT:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1046 continue
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1047 if ('\n' in text or '\r' in text) and token_type != tokenize.STRING:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1048 line_break = True
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1049 else:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1050 if (is_binary_operator(token_type, text) and line_break and
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1051 not unary_context):
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1052 yield start, "W503 line break before binary operator"
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1053 unary_context = text in '([{,;'
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1054 line_break = False
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1055
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1056
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1057 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
1058 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
1059
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1060 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
1061 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
1062
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1063 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
1064 E711: if arg != None:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1065 E711: if None == arg:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1066 E712: if arg == True:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1067 E712: if False == arg:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1068
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1069 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
1070 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
1071 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
1072 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
1073 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1074 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
1075 if match:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1076 singleton = match.group(1) or match.group(3)
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1077 same = (match.group(2) == '==')
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1078
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1079 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
1080 if singleton in ('None',):
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1081 code = 'E711'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1082 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1083 code = 'E712'
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1084 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