Skip to content

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

@anntzer

Description

@anntzer
Contributor

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

FichteFoll commented on Sep 10, 2017

@FichteFoll
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

anntzer commented on Sep 10, 2017

@anntzer
ContributorAuthor

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

FichteFoll commented on Sep 10, 2017

@FichteFoll
Contributor

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

flutefreak7 commented on May 14, 2020

@flutefreak7

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

asottile commented on May 14, 2020

@asottile
Member

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

changed the title [-]"continuation line over-indented" when comprehension is an argument[/-] [+]E127: "continuation line over-indented" when comprehension is an argument[/+] on Jun 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @FichteFoll@anntzer@asottile@flutefreak7

      Issue actions

        E127: "continuation line over-indented" when comprehension is an argument · Issue #680 · PyCQA/pycodestyle