Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E127: "continuation line over-indented" when comprehension is an argument #680

Open
anntzer opened this issue Sep 10, 2017 · 5 comments · May be fixed by #866
Open

E127: "continuation line over-indented" when comprehension is an argument #680

anntzer opened this issue Sep 10, 2017 · 5 comments · May be fixed by #866

Comments

@anntzer
Copy link
Contributor

anntzer commented Sep 10, 2017

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.)

@FichteFoll
Copy link
Contributor

I think the fundamental construct here is the conditional. The proposed indentation doesn't work outside of comprehensions either:

abc = (1 if a
            and 2
         else b)

I don't think PEP 8 mentions anything about this, but I haven't looked either.

@anntzer
Copy link
Contributor Author

anntzer commented 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
Copy link
Contributor

FichteFoll commented 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
Copy link

I'm getting a related problem with the following code:

import pandas as pd
data = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [0, 0, 0, 0, 0], 'C': [4, 5, 6, 7, 8]})
nonzero = (data != 0)
print(nonzero)

I get E128 continuation line underindented on the print(col) line. Looks like the parenthesis around data != 0 is confusing it somehow.

@asottile
Copy link
Member

@flutefreak7 I cannot reproduce, please create a new issue with more information (version, etc.)

@asottile asottile changed the title "continuation line over-indented" when comprehension is an argument E127: "continuation line over-indented" when comprehension is an argument Jun 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants