Add style for multiline comprehensions
See original GitHub issueIf a comprehension can’t fit on one line, splitting it to multiple lines on comprehension specific things would be good.
foo = [
some_func(value)
for value in a_long_named_iterable
if some_condition(value)
and not bad_thing(value)
]
Right now it’s collecting the for .. in .. if
all in one line rather than splitting them
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Pythonic Style for Multiline List Comprehension - Stack Overflow
PEP 8 kinda predates list comprehensions. I usually break these up over multiple lines at logical locations: memberdef_list = [elem for elem ...
Read more >Overusing list comprehensions and generator expressions in ...
In general, I prefer to write most of my comprehensions spaced out over multiple lines of code using the indentation style above.
Read more >Comprehensive Guide to Python List Comprehensions
This comprehensive guide to Python list comprehensions talks about the syntax, different forms, comparison to map, filter, ...
Read more >comprehensions poor formatting · Issue #733 · psf/black - GitHub
I will look into this but this is hard to get right. If the auto-formatter fails to format very complex expressions, I'd suggest...
Read more >Avoid More Than Two Expressions in List Comprehensions
The rule of thumb is to avoid using more than two expressions in a list comprehension. This could be two conditions, two loops,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this issue is still open. It sounds like #470 addressed where to split a long comprehension but does not allow forcing a complex comprehension to be split regardless of length.
👍 on this. Perhaps a knob for favoring shorter lines up to some limit would help. Or a knob simply enforcing that comprehensions are always split.