eric6/Plugins/CheckerPlugins/CodeStyleChecker/pycodestyle.py

Mon, 15 Jun 2020 19:48:51 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 15 Jun 2020 19:48:51 +0200
changeset 7620
52c2fe0308fd
parent 7360
9190402e4505
child 7639
422fd05e9c91
permissions
-rw-r--r--

Code Style Checker: updated pycodestyle to version 2.6.0.

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