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

Formatting list comprehensions #121

Open
hayd opened this issue Apr 22, 2015 · 3 comments
Open

Formatting list comprehensions #121

hayd opened this issue Apr 22, 2015 · 3 comments

Comments

@hayd
Copy link
Contributor

hayd commented Apr 22, 2015

My suggestion is the following:

  1. break in expression* => break the for and the if (edit: this is implied by 2.).
  2. break the for => break the if.

*This should add a continued_indent.

.1. seems obvious to me, I can see 2. being slightly more controversial...

To give a silly example:

# current
["This is a list comprehension which is quite long, I want it to split" +
 "a second long string pushes this over 79" for i in range(3) if i == 1]

# one possible
["This is a list comprehension which is quite long, I want it to split" +
     "a second long string pushes this over 79"
 for i in range(3) if i == 1]

# my suggestion
["This is a list comprehension which is quite long, I want it to split" +
     "a second long string pushes this over 79"
 for i in range(3)
 if i == 1]

# autopep8
["This is a list comprehension which is quite long, I want it to split" +
    "a second long string pushes this over 79" for i in range(3) if i == 1]

I realise @gwelymernans is working on list comprehensions, but thought I should punt this as an issue.

@bwendling
Copy link
Member

It's not a bad heuristic, I think. Though figuring out if there's a break in the expression might be complicated. The current way to figure out if a split occurred previous to the list comprehension is to backtrack through the tokens to find a split, which is sucky behavior if not done carefully.

@slippycheeze
Copy link

FWIW, I'd strongly prefer to be able to break before the "for" and "if" statements in a list comprehension, even if they could be packed to a single line, unless the whole thing fits on a single line. (and even then, I prefer separating them, because it matches better the "one statement per line" model of Python code, IMO, but I realise that's personal taste related.)

@msuozzo
Copy link
Member

msuozzo commented Nov 3, 2017

I think #470 may have introduced options to fix this.

I'd strongly prefer to be able to break before the "for" and "if" statements in a list comprehension

SPLIT_COMPLEX_COMPREHENSION should satisfy this preference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants