Plugins/CheckerPlugins/CodeStyleChecker/pycodestyle.py

Mon, 11 Feb 2019 22:22:12 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Mon, 11 Feb 2019 22:22:12 +0100
changeset 6732
1a4758379c45
parent 6645
ad476851d7e0
child 6733
519422827ace
permissions
-rw-r--r--

Updated pycodestyle to 2.5.0

3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1 #!/usr/bin/env python
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
4 # pycodestyle.py - Check Python source code formatting, according to
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
5 # PEP 8
5288
ff831e3e383e Update pycodestyle to official 2.1.0 version.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5147
diff changeset
6 #
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
7 # 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
8 # 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
9 # 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
10 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # 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
12 # 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
13 # (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
14 # 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
15 # 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
16 # 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
17 # subject to the following conditions:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 # The 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
20 # 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
21 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 # 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
23 # 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
24 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 # 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
26 # 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
27 # 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
28 # 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
29 # SOFTWARE.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
31 r"""
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
32 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
33
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 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
35 $ python pycodestyle.py -h
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 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
38 https://github.com/pycqa/pycodestyle
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 Groups of errors and warnings:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 E errors
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 W warnings
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 100 indentation
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 200 whitespace
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 300 blank lines
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 400 imports
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 500 line length
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 600 deprecation
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 700 statements
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
50 900 syntax error
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 """
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
52 from __future__ import with_statement
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 #
5147
d39dd5cee0c8 Renamed pep8.py to pycodestyle.py in order to track the upstream renaming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5146
diff changeset
55 # This is a modified version to make the original pycodestyle.py better suitable
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3629
diff changeset
56 # 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
57 # follows:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 # - made messages translatable via Qt
3670
f0cb7579c0b4 Finished renaming eric5 for PyQt5 to eric6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3629
diff changeset
60 # - added code for eric6 integration
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 #
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
62 # Copyright (c) 2011 - 2018 Detlev Offenbach <detlev@die-offenbachs.de>
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 #
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 import inspect
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 import keyword
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
67 import os
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
68 import re
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
69 import sys
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
70 import time
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 import tokenize
5288
ff831e3e383e Update pycodestyle to official 2.1.0 version.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5147
diff changeset
72 import warnings
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
73 import bisect
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
74
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
75 try:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
76 from functools import lru_cache
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
77 except ImportError:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
78 def lru_cache(maxsize=128): # noqa as it's a fake implementation.
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
79 """Does not really need a real a lru_cache, it's just
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
80 optimization, so let's just do nothing here. Python 3.2+ will
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
81 just get better performances, time to upgrade?
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
82 """
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
83 return lambda function: function
5288
ff831e3e383e Update pycodestyle to official 2.1.0 version.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5147
diff changeset
84
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
85 from fnmatch import fnmatch
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 from optparse import OptionParser
5288
ff831e3e383e Update pycodestyle to official 2.1.0 version.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5147
diff changeset
87
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 try:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
89 from configparser import RawConfigParser
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
90 from io import TextIOWrapper
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
91 except ImportError:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
92 from ConfigParser import RawConfigParser # __IGNORE_WARNING__
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
94 __version__ = '2.5.0-eric'
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
95
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
96 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox'
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
97 DEFAULT_IGNORE = 'E121,E123,E126,E226,E24,E704,W503,W504'
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
98 try:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
99 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
100 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
101 else:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
102 USER_CONFIG = os.path.join(
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
103 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
104 'pycodestyle'
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
105 )
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
106 except ImportError:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
107 USER_CONFIG = None
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
108
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
109 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
110 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
111 MAX_LINE_LENGTH = 79
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
112 # Number of blank lines between various code parts.
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
113 BLANK_LINES_CONFIG = {
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
114 # Top level class and function.
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
115 'top_level': 2,
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
116 # Methods and nested class and function.
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
117 'method': 1,
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
118 }
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
119 MAX_DOC_LENGTH = 72
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
120 REPORT_FORMAT = {
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
121 '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
122 '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
123 }
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
124
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
125 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
126 SINGLETONS = frozenset(['False', 'None', 'True'])
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
127 KEYWORDS = frozenset(keyword.kwlist + ['print', 'async']) - SINGLETONS
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
128 UNARY_OPERATORS = frozenset(['>>', '**', '*', '+', '-'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
129 ARITHMETIC_OP = frozenset(['**', '*', '/', '//', '+', '-'])
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
130 WS_OPTIONAL_OPERATORS = ARITHMETIC_OP.union(['^', '&', '|', '<<', '>>', '%'])
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
131 # Warn for -> function annotation operator in py3.5+ (issue 803)
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
132 FUNCTION_RETURN_ANNOTATION_OP = ['->'] if sys.version_info >= (3, 5) else []
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
133 WS_NEEDED_OPERATORS = frozenset([
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
134 '**=', '*=', '/=', '//=', '+=', '-=', '!=', '<>', '<', '>',
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
135 '%=', '^=', '&=', '|=', '==', '<=', '>=', '<<=', '>>=', '='] +
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
136 FUNCTION_RETURN_ANNOTATION_OP)
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
137 WHITESPACE = frozenset(' \t')
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
138 NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
139 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
140 # 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
141 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
142 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
143
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 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
145 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
146 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
147 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
148 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
149 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
150 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
151 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
152 r'\s*(?(1)|(None|False|True))\b')
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
153 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
154 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
155 r'|\s*\(\s*([^)]*[^ )])\s*\))')
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
156 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
157 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
158 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
159 HUNK_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$')
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
160 STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)\b')
5509
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
161 STARTSWITH_TOP_LEVEL_REGEX = re.compile(r'^(async\s+def\s+|def\s+|class\s+|@)')
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
162 STARTSWITH_INDENT_STATEMENT_REGEX = re.compile(
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
163 r'^\s*({0})\b'.format('|'.join(s.replace(' ', r'\s+') for s in (
5509
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
164 'def', 'async def',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
165 'for', 'async for',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
166 'if', 'elif', 'else',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
167 'try', 'except', 'finally',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
168 'with', 'async with',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
169 'class',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
170 'while',
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
171 )))
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
172 )
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
173 DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
175 _checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}}
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
176
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
177
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
178 def _get_parameters(function):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
179 if sys.version_info >= (3, 3):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
180 return [parameter.name
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
181 for parameter
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
182 in inspect.signature(function).parameters.values()
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
183 if parameter.kind == parameter.POSITIONAL_OR_KEYWORD]
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
184 else:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
185 return inspect.getargspec(function)[0]
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
186
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
187
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
188 def register_check(check, codes=None):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
189 """Register a new check object."""
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
190 def _add_check(check, kind, codes, args):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
191 if check in _checks[kind]:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
192 _checks[kind][check][0].extend(codes or [])
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
193 else:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
194 _checks[kind][check] = (codes or [''], args)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
195 if inspect.isfunction(check):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
196 args = _get_parameters(check)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
197 if args and args[0] in ('physical_line', 'logical_line'):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
198 if codes is None:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
199 codes = ERRORCODE_REGEX.findall(check.__doc__ or '')
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
200 _add_check(check, args[0], codes, args)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
201 elif inspect.isclass(check):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
202 if _get_parameters(check.__init__)[:2] == ['self', 'tree']:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
203 _add_check(check, 'tree', codes, None)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
204 return check
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
205
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
206
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
207 ########################################################################
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 # Plugins (check functions) for physical lines
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
209 ########################################################################
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
211 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 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
213 r"""Never mix tabs and spaces.
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 The most popular way of indenting Python is with spaces only. The
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
216 second-most popular way is with tabs only. Code indented with a
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
217 mixture of tabs and spaces should be converted to using spaces
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
218 exclusively. When invoking the Python command line interpreter with
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
219 the -t option, it issues warnings about code that illegally mixes
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
220 tabs and spaces. When using -tt these warnings become errors.
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
221 These options are highly recommended!
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
223 Okay: if a == 0:\n a = 1\n b = 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 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
225 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 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
227 for offset, char in enumerate(indent):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 if char != indent_char:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
229 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
230
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
232 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 def tabs_obsolete(physical_line):
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
234 r"""On 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
235
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 Okay: if True:\n return
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 W191: if True:\n\treturn
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 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
240 if '\t' in indent:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
241 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
242
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
244 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 def trailing_whitespace(physical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
246 r"""Trailing whitespace is superfluous.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
248 The warning returned varies on whether the line itself is blank,
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
249 for easier filtering for those who want to indent their blank lines.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
251 Okay: spam(1)\n#
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
252 W291: spam(1) \n#
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 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
254 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 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
256 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
257 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
258 stripped = physical_line.rstrip(' \t\v')
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 if physical_line != stripped:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 if stripped:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
261 return len(stripped), "W291 trailing whitespace"
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
263 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
264
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
266 @register_check
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
267 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
268 r"""Trailing blank lines are superfluous.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 Okay: spam(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 W391: spam(1)\n
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
272
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
273 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
274 """
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
275 if line_number == total_lines:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
276 stripped_last_line = physical_line.rstrip()
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
277 if physical_line and not stripped_last_line:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
278 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
279 if stripped_last_line == physical_line:
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
280 return len(lines[-1]), "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
281
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
283 @register_check
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
284 def maximum_line_length(physical_line, max_line_length, multiline,
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
285 line_number, noqa):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
286 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
287
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 There are still many devices around that are limited to 80 character
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
289 lines; plus, limiting windows to 80 characters makes it possible to
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
290 have several windows side-by-side. The default wrapping on such
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
291 devices looks ugly. Therefore, please limit all lines to a maximum
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
292 of 79 characters. For flowing long blocks of text (docstrings or
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
293 comments), limiting the 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
294
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
295 Reports error E501.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 line = physical_line.rstrip()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 length = len(line)
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
299 if length > max_line_length and not noqa:
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
300 # Special case: ignore long shebang lines.
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
301 if line_number == 1 and line.startswith('#!'):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
302 return
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
303 # Special case for long URLs in multi-line docstrings or
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
304 # comments, but still report the error when the 72 first chars
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
305 # are whitespaces.
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
306 chunks = line.split()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
307 if ((len(chunks) == 1 and multiline) or
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
308 (len(chunks) == 2 and chunks[0] == '#')) and \
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
309 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
310 return
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
311 if hasattr(line, 'decode'): # Python 2
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 # 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
313 try:
1145
2a16484e65c2 Fix for the PEP-8 checker to handle unicode better.
detlev@die-offenbachs.de
parents: 945
diff changeset
314 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
315 except UnicodeError:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
316 pass
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
317 if length > max_line_length:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
318 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
319 "(%d > %d characters)" % (length, max_line_length),
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
320 length, max_line_length)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
323 ########################################################################
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 # Plugins (check functions) for logical lines
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
325 ########################################################################
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
328 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 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
330 blank_before, previous_logical,
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
331 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
332 lines):
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
333 r"""Separate top-level function and class definitions with two blank
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
334 lines.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
336 Method definitions inside a class are separated by a single blank
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
337 line.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
339 Extra blank lines may be used (sparingly) to separate groups of
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
340 related functions. Blank lines may be omitted between a bunch of
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
341 related one-liners (e.g. a set of dummy implementations).
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
343 Use blank lines in functions, sparingly, to indicate logical
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
344 sections.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 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
347 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
348 Okay: def a():\n pass\n\n\n# Foo\n# Bar\n\ndef b():\n pass
5303
83ab5275f255 Take over pull request #593 to ignore variables starting with "def" or "class" on code E305.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5290
diff changeset
349 Okay: default = 1\nfoo = 1
83ab5275f255 Take over pull request #593 to ignore variables starting with "def" or "class" on code E305.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5290
diff changeset
350 Okay: classify = 1\nfoo = 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 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
353 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
354 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
355 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
356 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
357 E305: def a():\n pass\na()
5509
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
358 E306: def a():\n def b():\n pass\n def c():\n pass
6263
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
359 E307: def a():\n def b():\n pass\n\n\n def c():\n pass
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
360 E308: def a():\n\n\n\n pass
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 """
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
362 top_level_lines = BLANK_LINES_CONFIG['top_level']
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
363 method_lines = BLANK_LINES_CONFIG['method']
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
364
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
365 if line_number < top_level_lines + 1 and not previous_logical:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 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
367 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
368 if blank_lines:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
369 yield 0, "E304 blank lines found after function decorator"
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
370 elif (blank_lines > top_level_lines or
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
371 (indent_level and blank_lines == method_lines + 1)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
372 ):
6263
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
373 if indent_level:
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
374 if previous_logical.strip().startswith(('def ', 'class ')):
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
375 yield (0, "E308 too many blank lines (%d)", blank_lines)
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
376 else:
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
377 yield (0, "E307 too many blank lines (%d) in a nested "
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
378 "scope, expected %d", blank_lines, method_lines)
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
379 else:
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
380 yield (0, "E303 too many blank lines (%d), expected %d",
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
381 blank_lines, top_level_lines)
5509
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
382 elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
383 # If this is a one-liner (i.e. the next line is not more
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
384 # indented), and the previous line is also not deeper
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
385 # (it would be better to check if the previous line is part
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
386 # of another def/class at the same level), don't require blank
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
387 # lines around this.
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
388 prev_line = lines[line_number - 2] if line_number >= 2 else ''
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
389 next_line = lines[line_number] if line_number < len(lines) else ''
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
390 if (expand_indent(prev_line) <= indent_level and
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
391 expand_indent(next_line) <= indent_level):
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
392 return
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 if indent_level:
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
394 if not (blank_before == method_lines or
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
395 previous_indent_level < indent_level or
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
396 DOCSTRING_REGEX.match(previous_logical)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
397 ):
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
398 ancestor_level = indent_level
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
399 nested = False
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
400 # Search backwards for a def ancestor or tree root
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
401 # (top level).
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
402 for line in lines[line_number - top_level_lines::-1]:
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
403 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
404 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
405 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
406 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
407 break
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
408 if nested:
6263
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
409 yield (0, "E306 expected %s blank lines before a "
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
410 "nested definition, found %d", method_lines,
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
411 blank_before)
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
412 else:
6263
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
413 yield (0, "E301 expected %s blank lines, found %d",
4dd53711d869 pycodestyle.py: enhanced the 'blank lines before' test to make the reported errors more specific (introduced new error codes E307 and E308)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6246
diff changeset
414 method_lines, blank_before)
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
415 elif blank_before != top_level_lines:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
416 yield (0, "E302 expected %s blank lines, found %d",
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
417 top_level_lines, blank_before)
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
418 elif (logical_line and
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
419 not indent_level and
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
420 blank_before != top_level_lines and
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
421 previous_unindented_logical_line.startswith(('def ', 'class '))
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
422 ):
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
423 yield (0, "E305 expected %s blank lines after " \
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
424 "class or function definition, found %d",
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
425 top_level_lines, blank_before)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
428 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 def extraneous_whitespace(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
430 r"""Avoid extraneous whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
432 Avoid extraneous whitespace in these situations:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 - Immediately inside parentheses, brackets or braces.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 - Immediately before a comma, semicolon, or colon.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 Okay: spam(ham[1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 E201: spam( ham[1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 E201: spam(ham[ 1], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 E201: spam(ham[1], { eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 E202: spam(ham[1], {eggs: 2} )
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 E202: spam(ham[1 ], {eggs: 2})
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 E202: spam(ham[1], {eggs: 2 })
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 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
445 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
446 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
447 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 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
450 text = match.group()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 char = text.strip()
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 found = match.start()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
453 if text == char + ' ':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
454 # assert char in '([{'
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
455 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
456 elif line[found - 1] != ',':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
457 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
458 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
459
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
460
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
461 @register_check
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
462 def whitespace_around_keywords(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
463 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
464
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
465 Okay: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
466 E271: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
467 E272: True and False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
468 E273: True and\tFalse
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
469 E274: True\tand False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
470 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
471 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
472 before, after = match.groups()
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
473
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
474 if '\t' in before:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
475 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
476 elif len(before) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
477 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
478
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
479 if '\t' in after:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
480 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
481 elif len(after) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
482 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
483
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
485 @register_check
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
486 def missing_whitespace_after_import_keyword(logical_line):
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
487 r"""Multiple imports in form from x import (a, b, c) should have
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
488 space between import statement and parenthesised name list.
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
489
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
490 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
491 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
492 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
493 """
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
494 line = logical_line
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
495 indicator = ' import('
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
496 if line.startswith('from '):
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
497 found = line.find(indicator)
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
498 if -1 < found:
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
499 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
500 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
501
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
502
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
503 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 def missing_whitespace(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
505 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
506
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 Okay: [a, b]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 Okay: (3,)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 Okay: a[1:4]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 Okay: a[:4]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 Okay: a[1:]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 Okay: a[1:4:2]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 E231: ['a','b']
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514 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
515 E231: [{'a':'b'}]
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 for index in range(len(line) - 1):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 char = line[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 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
521 before = line[:index]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
522 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
523 before.rfind('{') < before.rfind('['):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 continue # Slice syntax, no space required
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 if char == ',' and line[index + 1] == ')':
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 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
527 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
528
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
530 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 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
532 indent_level, previous_indent_level):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
533 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
534
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
535 For really old code that you don't want to mess up, you can continue
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
536 to use 8-space tabs.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 Okay: a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 Okay: if a == 0:\n a = 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 E111: a = 1
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
541 E114: # a = 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 Okay: for item in items:\n pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 E112: for item in items:\npass
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
545 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
546
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 Okay: a = 1\nb = 2
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 E113: a = 1\n b = 2
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
549 E116: a = 1\n # b = 2
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 """
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
551 c = 0 if logical_line else 3
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
552 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
553 if indent_level % 4:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
554 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
555 indent_expect = previous_logical.endswith(':')
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 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
557 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
558 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
559 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
560
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
561 expected_indent_level = previous_indent_level + 4
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
562 if indent_expect and indent_level > expected_indent_level:
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
563 yield 0, tmpl % (7, 'over-indented')
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
564
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
565
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
566 @register_check
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
567 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
568 indent_char, noqa, verbose):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
569 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
570
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
571 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
572 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
573 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
574
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
575 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
576 - there should be no arguments on the first line, and
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
577 - further indentation should be used to clearly distinguish itself
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
578 as a continuation line.
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
579
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
580 Okay: a = (\n)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
581 E123: a = (\n )
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
582
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
583 Okay: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
584 E121: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
585 E122: a = (\n42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
586 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
587 E124: a = (24,\n 42\n)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
588 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
589 E126: a = (\n 42)
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
590 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
591 E128: a = (24,\n 42)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
592 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
593 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
594 """
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
595 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
596 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
597 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
598 return
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
599
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
600 # 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
601 # 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
602 # 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
603 # 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
604 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
605
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
606 row = depth = 0
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
607 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
608 # 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
609 parens = [0] * nrows
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
610 # 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
611 rel_indent = [0] * nrows
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
612 # 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
613 open_rows = [[0]]
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
614 # for each depth, memorize the hanging indentation
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
615 hangs = [None]
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
616 # visual indents
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
617 indent_chances = {}
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
618 last_indent = tokens[0][2]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
619 visual_indent = None
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
620 last_token_multiline = False
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
621 # 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
622 indent = [last_indent[1]]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
623 if verbose >= 3:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
624 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
625
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
626 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
627
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
628 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
629 if newline:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
630 row = start[0] - first_row
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
631 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
632
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
633 if newline:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
634 # 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
635 last_indent = start
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
636 if verbose >= 3:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
637 print("... " + line.rstrip())
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
638
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
639 # record the initial indent.
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
640 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
641
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
642 # identify closing bracket
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
643 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
644
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
645 # 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
646 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
647 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
648 hanging_indent = hang in valid_hangs
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
649 if hanging_indent:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
650 break
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
651 if hangs[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
652 hanging_indent = (hang == hangs[depth])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
653 # 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
654 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
655 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
656
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
657 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
658 # 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
659 if start[1] != indent[depth]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
660 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
661 "visual indentation")
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
662 elif close_bracket and not hang:
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
663 # closing bracket matches indentation of opening
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
664 # bracket's line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
665 if hang_closing:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
666 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
667 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
668 if visual_indent is not True:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
669 # visual indent is broken
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
670 yield (start, "E128 continuation line "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
671 "under-indented for visual indent")
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
672 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
673 # hanging indent is verified
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
674 if close_bracket and not hang_closing:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
675 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
676 "indentation of opening bracket's line")
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
677 hangs[depth] = hang
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
678 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
679 # visual indent is verified
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
680 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
681 elif visual_indent in (text, str):
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
682 # ignore token lined up with matching one from a
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
683 # previous line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
684 pass
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
685 else:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
686 # indent is broken
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
687 if hang <= 0:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
688 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
689 elif indent[depth]:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
690 error = "E127", "over-indented for visual indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
691 elif not close_bracket and hangs[depth]:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
692 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
693 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
694 hangs[depth] = hang
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
695 if hang > 4:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
696 error = "E126", "over-indented for hanging indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
697 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
698 error = "E121", "under-indented for hanging indent"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
699 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
700
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
701 # look for visual indenting
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
702 if (parens[row] and
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
703 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
704 not indent[depth]):
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
705 indent[depth] = start[1]
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
706 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
707 if verbose >= 4:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
708 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
709 # 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
710 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
711 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
712 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
713 # 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
714 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
715 indent_chances[end[1] + 1] = True
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
716 elif text == ':' and line[end[1]:].isspace():
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
717 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
718
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
719 # 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
720 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
721 if text in '([{':
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
722 depth += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
723 indent.append(0)
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
724 hangs.append(None)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
725 if len(open_rows) == depth:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
726 open_rows.append([])
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
727 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
728 parens[row] += 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
729 if verbose >= 4:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
730 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
731 (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
732 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
733 # 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
734 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
735 hangs.pop()
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
736 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
737 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
738 indent[d] = 0
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
739 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
740 if ind >= prev_indent:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
741 del indent_chances[ind]
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
742 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
743 depth -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
744 if depth:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
745 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
746 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
747 if parens[idx]:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
748 parens[idx] -= 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
749 break
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
750 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
751 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
752 # 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
753 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
754
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
755 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
756 if last_token_multiline:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
757 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
758
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
759 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
760 pos = (start[0], indent[0] + 4)
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
761 if visual_indent:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
762 code = "E129 visually indented line"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
763 else:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
764 code = "E125 continuation line"
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
765 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
766
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
768 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 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
770 r"""Avoid extraneous whitespace.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
772 Avoid extraneous whitespace in the following situations:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
773 - 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
774 function call.
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
775 - 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
776
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 Okay: spam(1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 E211: spam (1)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 Okay: dict['key'] = list[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 E211: dict ['key'] = list[index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 E211: dict['key'] = list [index]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
784 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
785 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
786 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
787 if (token_type == tokenize.OP and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 text in '([' and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 start != prev_end and
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 (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
791 # 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
792 (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
793 # 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
794 not keyword.iskeyword(prev_text)):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
795 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
796 prev_type = token_type
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 prev_text = text
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
801 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 def whitespace_around_operator(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
803 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
804
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 Okay: a = 12 + 3
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 E221: a = 4 + 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 E222: a = 4 + 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 E223: a = 4\t+ 5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809 E224: a = 4 +\t5
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 """
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
811 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
812 before, after = match.groups()
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
814 if '\t' in before:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
815 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
816 elif len(before) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
817 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
818
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
819 if '\t' in after:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
820 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
821 elif len(after) > 1:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
822 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
823
3209
c5432abceb25 CodeStyleChecker moved to background service and done a little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3145
diff changeset
824
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
825 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 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
827 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
828
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829 - 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
830 either side: assignment (=), augmented assignment (+=, -= etc.),
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
831 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
832 Booleans (and, or, not).
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
834 - 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
835 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
836
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 Okay: i = i + 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
838 Okay: submitted += 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
839 Okay: x = x * 2 - 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840 Okay: hypot2 = x * x + y * y
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841 Okay: c = (a + b) * (a - b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 Okay: foo(bar, key='word', *args, **kwargs)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
843 Okay: alpha[:-i]
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845 E225: i=i+1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846 E225: submitted +=1
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
847 E225: x = x /2 - 1
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848 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
849 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
850 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
851 E227: c = a|b
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
852 E228: msg = fmt%(errno, errmsg)
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 parens = 0
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 need_space = False
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 prev_type = tokenize.OP
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857 prev_text = prev_end = None
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
858 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
859 if token_type in SKIP_COMMENTS:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 continue
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 if text in ('(', 'lambda'):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862 parens += 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 elif text == ')':
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 parens -= 1
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 if need_space:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866 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
867 # 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
868 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
869 yield (need_space[0],
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
870 "E225 missing whitespace around operator")
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871 need_space = False
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
872 elif text == '>' and prev_text in ('<', '-'):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
873 # 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
874 # 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
875 pass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
876 else:
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
877 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
878 # 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
879 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
880 elif prev_text != '**':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
881 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
882 if prev_text == '%':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
883 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
884 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
885 code, optype = 'E227', 'bitwise or shift'
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
886 yield (need_space[0], "%s missing whitespace "
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
887 "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
888 need_space = False
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
889 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
890 if text == '=' and parens:
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
891 # 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
892 pass
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
893 elif text in WS_NEEDED_OPERATORS:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 need_space = True
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 elif text in UNARY_OPERATORS:
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
896 # Check if the operator is used as a binary operator
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 # Allow unary operators: -123, -x, +1.
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
898 # Allow argument unpacking: foo(*args, **kwargs).
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
899 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
900 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
901 need_space = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
902 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
903 need_space = None
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
904
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
905 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
906 # 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
907 # 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
908 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
909 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
910 # 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
911 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
912 need_space = False
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 prev_type = token_type
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914 prev_text = text
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
918 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 def whitespace_around_comma(logical_line):
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
920 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
921
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 Note: these checks are disabled by default
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924 Okay: a = (1, 2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 E241: a = (1, 2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
926 E242: a = (1,\t2)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 line = logical_line
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
929 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
930 found = m.start() + 1
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
931 if '\t' in m.group():
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
932 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
933 else:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
934 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
935
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
937 @register_check
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
938 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
939 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
940
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 Don't use spaces around the '=' sign when used to indicate a
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
942 keyword argument or a default parameter value, except when
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
943 using a type annotation.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 Okay: def complex(real, imag=0.0):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
946 Okay: return magic(r=real, i=imag)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
947 Okay: boolean(a == b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948 Okay: boolean(a != b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 Okay: boolean(a <= b)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 Okay: boolean(a >= b)
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
951 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
952 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
953
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 E251: def complex(real, imag = 0.0):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
955 E251: return magic(r = real, i = imag)
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
956 E252: def complex(real, image: float=0.0):
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
957 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 parens = 0
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
959 no_space = False
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
960 require_space = False
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
961 prev_end = None
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
962 annotated_func_arg = False
5509
d4c4763b46ee Upgraded pycodestyle to version 2.3.1
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 5435
diff changeset
963 in_def = bool(STARTSWITH_DEF_REGEX.match(logical_line))
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
964
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
965 message = "E251 unexpected spaces around keyword / parameter equals"
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
966 missing_message = "E252 missing whitespace around parameter equals"
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
967
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
968 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
969 if token_type == tokenize.NL:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
970 continue
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
971 if no_space:
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
972 no_space = False
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
973 if start != prev_end:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
974 yield (prev_end, message)
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
975 if require_space:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
976 require_space = False
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
977 if start == prev_end:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
978 yield (prev_end, missing_message)
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
979 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
980 if text in '([':
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
981 parens += 1
5146
f96c32abd120 Updated the pep8 checker to version 2.1.0dev0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4828
diff changeset
982 elif text in ')]':
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
983 parens -= 1
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
984 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
985 annotated_func_arg = True
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
986 elif parens == 1 and text == ',':
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
987 annotated_func_arg = False
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
988 elif parens and text == '=':
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
989 if annotated_func_arg and parens == 1:
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
990 require_space = True
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
991 if start == prev_end:
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
992 yield (prev_end, missing_message)
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
993 else:
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
994 no_space = True
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
995 if start != prev_end:
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
996 yield (prev_end, message)
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
997 if not parens:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
998 annotated_func_arg = False
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
999
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1000 prev_end = end
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1001
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1002
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
1003 @register_check
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1004 def whitespace_before_comment(logical_line, tokens):
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1005 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
1006
6732
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
1007 An inline comment is a comment on the same line as a statement.
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
1008 Inline comments should be separated by at least two spaces from the
1a4758379c45 Updated pycodestyle to 2.5.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6645
diff changeset
1009 statement. They should start with a # and a single space.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1010
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1011 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
1012 (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
1013
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 Okay: x = x + 1 # Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1015 Okay: x = x + 1 # Increment x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1016 Okay: # Block comment
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1017 E261: x = x + 1 # Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1018 E262: x = x + 1 #Increment x
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1019 E262: x = x + 1 # Increment x
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1020 E265: #Block comment
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1021 E266: ### Block comment
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1022 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1023 prev_end = (0, 0)
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1024 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
1025 if token_type == tokenize.COMMENT:
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1026 inline_comment = line[:start[1]].strip()
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1027 if inline_comment:
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1028 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
1029 yield (prev_end,
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1030 "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
1031 symbol, sp, comment = text.partition(' ')
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1032 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
1033 if inline_comment:
4828
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1034 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
1035 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
1036 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
1037 if bad_prefix != '#':
3616
081b69e00f3e updated pep8 to 1.5.6
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3484
diff changeset
1038 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
1039 elif comment:
b313794f46a2 updated pep8 to 1.7.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 4631
diff changeset
1040 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
1041 elif token_type != tokenize.NL:
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1042 prev_end = end
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1043
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1044
6246
fe07a9f16f23 Updated pycodestyle to 2.4.0
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 6048
diff changeset
1045 @register_check
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1046 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
1047 r"""Place imports on separate lines.
832
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1048
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1049 Okay: import os\nimport sys
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1050 E401: import sys, os
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1051
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1052 Okay: from subprocess import Popen, PIPE
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1053 Okay: from myclas import MyClass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1054 Okay: from foo.bar.yourclass import YourClass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1055 Okay: import myclass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1056 Okay: import foo.bar.yourclass
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1057 """
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1058 line = logical_line
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1059 if line.startswith('import '):
eb5ff61f927b Added a checker for PEP 8 compliance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1060 found = line.find(',')
2862
a1448560d7dc Started upgrading the pep8 checker to the latest release.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2861
diff changeset
1061 if -1 < found and ';