Plugins/CheckerPlugins/CodeStyleChecker/pycodestyle.py

Fri, 13 Apr 2018 22:32:32 +0200

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Fri, 13 Apr 2018 22:32:32 +0200
changeset 6246
fe07a9f16f23
parent 6048
82ad8ec9548c
child 6263
4dd53711d869
permissions
-rw-r--r--

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