Plugins/CheckerPlugins/CodeStyleChecker/pep8.py

Sun, 04 Sep 2016 16:50:23 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 04 Sep 2016 16:50:23 +0200
changeset 5146
f96c32abd120
parent 4828
b313794f46a2
permissions
-rw-r--r--

Updated the pep8 checker to version 2.1.0dev0.

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