Skip to content

Commit f3d7736

Browse files
committed
Merge pull request #207 from jcrocholl/issue103
Fix issue #103 - closing bracket indentation gains flexibility
2 parents e0d2d95 + 54a7d09 commit f3d7736

4 files changed

Lines changed: 33 additions & 14 deletions

File tree

CHANGES.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ Changelog
1717

1818
* Correctly report other E12 errors when E123 is ignored. (Issue #103)
1919

20+
* New option ``--hang-closing`` to switch to the alternative style of
21+
closing bracket indentation for hanging indent. Add error E133 for
22+
closing bracket which is missing indentation. (Issue #103)
23+
24+
* Accept both styles of closing bracket indentation for hanging indent.
25+
Do not report error E123 in the default configuration. (Issue #103)
26+
2027
* Do not crash when running AST checks and the document contains null bytes.
2128
(Issue #184)
2229

docs/intro.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Quick help is available on the command line::
134134
--count print total number of errors and warnings to standard
135135
error and set exit code to 1 if total is not null
136136
--max-line-length=n set maximum allowed line length (default: 79)
137+
--hang-closing hang closing bracket instead of matching indentation of
138+
opening bracket's line
137139
--format=format set the error format [default|pylint|<custom>]
138140
--diff report only lines changed according to the unified diff
139141
received on STDIN
@@ -145,8 +147,8 @@ Quick help is available on the command line::
145147
The project options are read from the [pep8] section of the tox.ini
146148
file or the setup.cfg file located in any parent folder of the path(s)
147149
being processed. Allowed options are: exclude, filename, select,
148-
ignore, max-line-length, count, format, quiet, show-pep8, show-source,
149-
statistics, verbose.
150+
ignore, max-line-length, hang-closing, count, format, quiet, show-pep8,
151+
show-source, statistics, verbose.
150152

151153
--config=path user config file location (default: ~/.config/pep8)
152154

@@ -195,7 +197,7 @@ This is the current list of error and warning codes:
195197
+----------+----------------------------------------------------------------------+
196198
| E122 (^) | continuation line missing indentation or outdented |
197199
+----------+----------------------------------------------------------------------+
198-
| E123 (^) | closing bracket does not match indentation of opening bracket's line |
200+
| E123 (*) | closing bracket does not match indentation of opening bracket's line |
199201
+----------+----------------------------------------------------------------------+
200202
| E124 (^) | closing bracket does not match visual indentation |
201203
+----------+----------------------------------------------------------------------+
@@ -207,6 +209,7 @@ This is the current list of error and warning codes:
207209
+----------+----------------------------------------------------------------------+
208210
| E128 (^) | continuation line under-indented for visual indent |
209211
+----------+----------------------------------------------------------------------+
212+
| E133 (*) | closing bracket is missing indentation |
210213
+----------+----------------------------------------------------------------------+
211214
| **E2** | *Whitespace* |
212215
+----------+----------------------------------------------------------------------+
@@ -337,9 +340,11 @@ This is the current list of error and warning codes:
337340
| W604 | backticks are deprecated, use 'repr()' |
338341
+----------+----------------------------------------------------------------------+
339342

340-
**(*)** In the default configuration, the checks **E226**, **E241**
341-
and **E242** are ignored because they are not rules unanimously accepted,
342-
and `PEP 8`_ does not enforce them.
343+
**(*)** In the default configuration, the checks **E123**, **E133**, **E226**,
344+
**E241** and **E242** are ignored because they are not rules unanimously
345+
accepted, and `PEP 8`_ does not enforce them. The check **E133** is mutually
346+
exclusive with check **E123**. Use switch ``--hang-closing`` to report **E133**
347+
instead of **E123**.
343348

344349
**(^)** These checks can be disabled at the line level using the ``# noqa``
345350
special comment. This possibility should be reserved for special cases.

pep8.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
from ConfigParser import RawConfigParser
6464

6565
DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__'
66-
DEFAULT_IGNORE = 'E226,E24'
66+
DEFAULT_IGNORE = 'E123,E226,E24'
6767
if sys.platform == 'win32':
6868
DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8')
6969
else:
@@ -381,7 +381,8 @@ def indentation(logical_line, previous_logical, indent_char,
381381
yield 0, "E113 unexpected indentation"
382382

383383

384-
def continued_indentation(logical_line, tokens, indent_level, noqa, verbose):
384+
def continued_indentation(logical_line, tokens, indent_level, hang_closing,
385+
noqa, verbose):
385386
r"""
386387
Continuation lines should align wrapped elements either vertically using
387388
Python's implicit line joining inside parentheses, brackets and braces, or
@@ -467,7 +468,8 @@ def continued_indentation(logical_line, tokens, indent_level, noqa, verbose):
467468
"visual indentation")
468469
elif close_bracket and not hang:
469470
# closing bracket matches indentation of opening bracket's line
470-
pass
471+
if hang_closing:
472+
yield start, "E133 closing bracket is missing indentation"
471473
elif visual_indent is True:
472474
# visual indent is verified
473475
if not indent[depth]:
@@ -481,7 +483,7 @@ def continued_indentation(logical_line, tokens, indent_level, noqa, verbose):
481483
"under-indented for visual indent")
482484
elif hang == 4 or (indent_next and rel_indent[row] == 8):
483485
# hanging indent is verified
484-
if close_bracket:
486+
if close_bracket and not hang_closing:
485487
yield (start, "E123 closing bracket does not match "
486488
"indentation of opening bracket's line")
487489
else:
@@ -1184,6 +1186,7 @@ def __init__(self, filename=None, lines=None,
11841186
self._logical_checks = options.logical_checks
11851187
self._ast_checks = options.ast_checks
11861188
self.max_line_length = options.max_line_length
1189+
self.hang_closing = options.hang_closing
11871190
self.verbose = options.verbose
11881191
self.filename = filename
11891192
if filename is None:
@@ -1694,8 +1697,9 @@ def get_parser(prog='pep8', version=__version__):
16941697
parser = OptionParser(prog=prog, version=version,
16951698
usage="%prog [options] input ...")
16961699
parser.config_options = [
1697-
'exclude', 'filename', 'select', 'ignore', 'max-line-length', 'count',
1698-
'format', 'quiet', 'show-pep8', 'show-source', 'statistics', 'verbose']
1700+
'exclude', 'filename', 'select', 'ignore', 'max-line-length',
1701+
'hang-closing', 'count', 'format', 'quiet', 'show-pep8',
1702+
'show-source', 'statistics', 'verbose']
16991703
parser.add_option('-v', '--verbose', default=0, action='count',
17001704
help="print status messages, or debug with -vv")
17011705
parser.add_option('-q', '--quiet', default=0, action='count',
@@ -1730,6 +1734,9 @@ def get_parser(prog='pep8', version=__version__):
17301734
default=MAX_LINE_LENGTH,
17311735
help="set maximum allowed line length "
17321736
"(default: %default)")
1737+
parser.add_option('--hang-closing', action='store_true',
1738+
help="hang closing bracket instead of matching "
1739+
"indentation of opening bracket's line")
17331740
parser.add_option('--format', metavar='format', default='default',
17341741
help="set the error format [default|pylint|<custom>]")
17351742
parser.add_option('--diff', action='store_true',

testsuite/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_styleguide_options(self):
164164
self.assertEqual(pep8style.options.filename, ['*.py'])
165165
self.assertEqual(pep8style.options.format, 'default')
166166
self.assertEqual(pep8style.options.select, ())
167-
self.assertEqual(pep8style.options.ignore, ('E226', 'E24'))
167+
self.assertEqual(pep8style.options.ignore, ('E123', 'E226', 'E24'))
168168
self.assertEqual(pep8style.options.max_line_length, 79)
169169

170170
def test_styleguide_ignore_code(self):
@@ -178,7 +178,7 @@ def parse_argv(argstring):
178178

179179
options = parse_argv('').options
180180
self.assertEqual(options.select, ())
181-
self.assertEqual(options.ignore, ('E226', 'E24'))
181+
self.assertEqual(options.ignore, ('E123', 'E226', 'E24'))
182182

183183
options = parse_argv('--doctest').options
184184
self.assertEqual(options.select, ())

0 commit comments

Comments
 (0)