@@ -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
0 commit comments