Skip to content

Commit ace7b01

Browse files
committed
Improve check for logical continuation in comprehension.
Excludes ``` sorted(obj for obj in iterator if some_long_cond() and some_other_cond()) ``` from E127. It is true that this also lets through some "bad" indents e.g. ``` (1 if a and 2 else b) ``` but at the tokenization level it is quite difficult to distinguish between the "if" in `a for b in c if d` and the "if" in `a if b else c`, which play quite different roles.
1 parent 68cc24f commit ace7b01

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pycodestyle.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
608608
if verbose >= 3:
609609
print(">>> " + tokens[0][4].rstrip())
610610

611-
for token_type, text, start, end, line in tokens:
611+
for idx, (token_type, text, start, end, line) in enumerate(tokens):
612612

613613
newline = row < start[0] - first_row
614614
if newline:
@@ -695,6 +695,10 @@ def continued_indentation(logical_line, tokens, indent_level, hang_closing,
695695
elif (token_type in (tokenize.STRING, tokenize.COMMENT) or
696696
text in ('u', 'ur', 'b', 'br')):
697697
indent_chances[start[1]] = str
698+
# hanging indents in comprehensions
699+
elif (token_type == tokenize.NAME and text == "if" and
700+
tokens[idx + 1][0] != tokenize.LPAR): # [0] == .type
701+
indent_chances[tokens[idx + 1][2][1]] = True # [2] == .start
698702
# special case for the "if" statement because len("if (") == 4
699703
elif not indent_chances and not row and not depth and text == 'if':
700704
indent_chances[end[1] + 1] = True

testsuite/E12not.py

+4
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,7 @@ def other_example():
639639
# more stuff
640640
)
641641
)
642+
#: W503
643+
sorted(obj for obj in []
644+
if True
645+
and False)

0 commit comments

Comments
 (0)