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

Lone comprehensions without additional indentation #2830

Closed
francois-rozet opened this issue Jan 30, 2022 · 2 comments
Closed

Lone comprehensions without additional indentation #2830

francois-rozet opened this issue Jan 30, 2022 · 2 comments
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?

Comments

@francois-rozet
Copy link

francois-rozet commented Jan 30, 2022

Describe the style change

Comprehensions that are alone within a call (or similar) would be left without indentation.

This change can be considered as consistency breaking, so I would not be surprised if it was rejected.

Examples in the current Black style

something(
    [
        a,
        b,
        c,
    ]
)

[
     [
        a,
        b,
        c,
    ]
]

np.stack(
    [
        np.arange(0, i)
        for i in range(0, 666)
        if isprime(i)
    ]
)

Desired style

something([
    a,
    b,
    c,
])

[[
    a,
    b,
    c,
]]

np.stack([
    np.arange(0, i)
    for i in range(0, 666)
    if isprime(i)
])

Additional context

A lot of functions take a single iterable as input, like join, list.extend, dict.update, numpy.stack, ... For those, indenting the argument when it is a comprehension makes the code less readable IMO.

A good example is creating 2d NumPy arrays:

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
])

becomes, after Black auto-format,

A = np.array(
    [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ]
)

Another argument in favor of this change is that switching from a comprehension to a generator changes dramatically the formatting but not the meaning. For instance,

join([
    formating_function_with_a_very_long_name(i)
    for i in range(123456789)
    if isprime(i)
    if isperfect(i)
])

and

join(
    formating_function_with_a_very_long_name(i)
    for i in range(123456789)
    if isprime(i)
    if isperfect(i)
)

are formatted differently, even though they are quite similar semantically.

@francois-rozet francois-rozet added the T: style What do we want Blackened code to look like? label Jan 30, 2022
@felix-hilden
Copy link
Collaborator

We've discussed this in #1811, so I'll close this as a duplicate! But do point out if anything you suggest here isn't covered. Most of it should be.

@felix-hilden felix-hilden added F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists labels Jan 30, 2022
@francois-rozet
Copy link
Author

Thanks! I didn't know what keywords to look for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

2 participants