Skip to content

Commit e921888

Browse files
committed
Report E131 instead of E121 / E126 for unaligned hanging indent
1 parent fed43c5 commit e921888

4 files changed

Lines changed: 26 additions & 9 deletions

File tree

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Changes:
2525
for hanging indent" instead of indentation not being a
2626
multiple of 4.
2727

28+
* Report E131 instead of E121 / E126 if the hanging indent is not
29+
consistent within the same continuation block. It helps when
30+
error E121 or E126 is in the ``ignore`` list.
31+
2832
* Report E126 instead of E121 when the continuation line is hanging
2933
with extra indentation, even if indentation is not a multiple of 4.
3034

docs/intro.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ This is the current list of error and warning codes:
212212
+----------+----------------------------------------------------------------------+
213213
| E129 (^) | visually indented line with same indent as next logical line |
214214
+----------+----------------------------------------------------------------------+
215+
| E131 (^) | continuation line unaligned for hanging indent |
216+
+----------+----------------------------------------------------------------------+
215217
| E133 (*) | closing bracket is missing indentation |
216218
+----------+----------------------------------------------------------------------+
217219
+----------+----------------------------------------------------------------------+

pep8.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
416416
E127: a = (24,\n 42)
417417
E128: a = (24,\n 42)
418418
E129: if (a or\n b):\n pass
419+
E131: a = (\n 42\n 24)
419420
"""
420421
first_row = tokens[0][2][0]
421422
nrows = 1 + tokens[-1][2][0] - first_row
@@ -436,6 +437,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
436437
rel_indent = [0] * nrows
437438
# for each depth, collect a list of opening rows
438439
open_rows = [[0]]
440+
# for each depth, memorize the hanging indentation
441+
hangs = [None]
439442
# visual indents
440443
indent_chances = {}
441444
last_indent = tokens[0][2]
@@ -470,6 +473,8 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
470473
hanging_indent = hang in valid_hangs
471474
if hanging_indent:
472475
break
476+
if hangs[depth]:
477+
hanging_indent = (hang == hangs[depth])
473478
# is there any chance of visual indent?
474479
visual_indent = (not close_bracket and hang > 0 and
475480
indent_chances.get(start[1]))
@@ -493,10 +498,10 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
493498
if close_bracket and not hang_closing:
494499
yield (start, "E123 closing bracket does not match "
495500
"indentation of opening bracket's line")
501+
hangs[depth] = hang
496502
elif visual_indent is True:
497503
# visual indent is verified
498-
if not indent[depth]:
499-
indent[depth] = start[1]
504+
indent[depth] = start[1]
500505
elif visual_indent in (text, str):
501506
# ignore token lined up with matching one from a previous line
502507
pass
@@ -506,10 +511,14 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
506511
error = "E122", "missing indentation or outdented"
507512
elif indent[depth]:
508513
error = "E127", "over-indented for visual indent"
509-
elif hang > 4:
510-
error = "E126", "over-indented for hanging indent"
514+
elif not close_bracket and hangs[depth]:
515+
error = "E131", "unaligned for hanging indent"
511516
else:
512-
error = "E121", "under-indented for hanging indent"
517+
hangs[depth] = hang
518+
if hang > 4:
519+
error = "E126", "over-indented for hanging indent"
520+
else:
521+
error = "E121", "under-indented for hanging indent"
513522
yield start, "%s continuation line %s" % error
514523

515524
# look for visual indenting
@@ -534,6 +543,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
534543
if text in '([{':
535544
depth += 1
536545
indent.append(0)
546+
hangs.append(None)
537547
if len(open_rows) == depth:
538548
open_rows.append([])
539549
open_rows[depth].append(row)
@@ -544,6 +554,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
544554
elif text in ')]}' and depth > 0:
545555
# parent indents should not be more than this one
546556
prev_indent = indent.pop() or last_indent[1]
557+
hangs.pop()
547558
for d in range(depth):
548559
if indent[d] > prev_indent:
549560
indent[d] = 0

testsuite/E12.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
'reasonComment_de', 'reasonComment_it'),
5959
'?'),
6060
"foo")
61-
#: E126 E126
61+
#: E126
6262
abricot = 3 + \
6363
4 + \
6464
5 + 6
65-
#: E126
65+
#: E131
6666
print "hello", (
6767

6868
"there",
@@ -208,7 +208,7 @@ def qualify_by_address(
208208
1, 2, 3,
209209
4, 5, 6,
210210
]
211-
#: E126 E126
211+
#: E126
212212
abris = 3 + \
213213
4 + \
214214
5 + 6
@@ -242,7 +242,7 @@ def qualify_by_address(
242242
) or
243243
y == 4):
244244
pass
245-
#: E126
245+
#: E131
246246
troublesome_hash = {
247247
"hash": "value",
248248
"long": "the quick brown fox jumps over the lazy dog before doing a "

0 commit comments

Comments
 (0)