hhatto / autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
https://pypi.org/project/autopep8/
MIT License
4.58k stars 291 forks source link

Unfortunate E501 fix for list comprehension #586

Open maltekliemann opened 3 years ago

maltekliemann commented 3 years ago

Python Code

# example.py
[very_long_item_name for very_long_item_name in function(something) if condition]

Result

[very_long_item_name for very_long_item_name in function(
    something) if condition]

This is hard to read. Even if function has more arguments, this looks confusing to me. I would have expected any of the following (depending on the length of function(something) and condition):

# Better solution?
[very_long_item_name for very_long_item_name
    in function(something) if condition]

# ... or?
[very_long_item_name for very_long_item_name in
    function(something) if condition]

# For long `function(something)` or `condition`?
[very_long_item_name
    for very_long_item_name
    in function(something)
    if condition]

# ...or?
[very_long_item_name for
    very_long_item_name in
    function(something) if
    condition]

(I prefer the first and third solution.) Or is this a situation where the "correct" solution is to rewrite the comprehension using loops?

Command Line and Configuration

No configuration.

Command Line

$ autopep8 example.py

Your Environment