-
Notifications
You must be signed in to change notification settings - Fork 750
Description
Consider
[obj for obj in iterator
if some_long_cond()
and some_other_cond()]
(obj for obj in iterator
if some_long_cond()
and some_other_cond())
sorted(obj for obj in iterator
if some_long_cond()
and some_other_cond())
I would consider that this is a reasonable, and perhaps even the correct way to indent such expressions, assuming you adhere to the "binary operators after linebreak" school and also assuming that the expressions are too long to fit in a single line.
Interestingly, pycodestyle is happy with the indenting of the first two expressions, but complains about the last one
test.py:9:11: E127 continuation line over-indented for visual indent
(Note that it is able to recognize the correct amount of indenting needed in the first two expressions -- adding or removing a space before "and" results in the same warning as for the third expression.)
Edit: note to self or whoever will work on this: the first two cases actually work "accidentally" because the indent of the last line is 4 characters, which is always accepted -- prepending the thing e.g. with the_list = [...
and indenting the rest to align makes the thing fail again.)
Activity
FichteFoll commentedon Sep 10, 2017
I think the fundamental construct here is the conditional. The proposed indentation doesn't work outside of comprehensions either:
I don't think PEP 8 mentions anything about this, but I haven't looked either.
anntzer commentedon Sep 10, 2017
The conditional in
(foo for foo in bar if quux)
and in
(foo if bar else quux)
are quite different objects (one is a clause to a comprehension, the other a ternary).
FichteFoll commentedon Sep 10, 2017
That is correct. I was just thinking that inline
if
wasn't considered for indentation rules at all and pointing to another usage of it.flutefreak7 commentedon May 14, 2020
I'm getting a related problem with the following code:
I get E128 continuation line underindented on the print(col) line. Looks like the parenthesis around
data != 0
is confusing it somehow.asottile commentedon May 14, 2020
@flutefreak7 I cannot reproduce, please create a new issue with more information (version, etc.)
[-]"continuation line over-indented" when comprehension is an argument[/-][+]E127: "continuation line over-indented" when comprehension is an argument[/+]