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

Less whitespace when a function call contains a single list/dict/tuple/string #3621

Open
st-pasha opened this issue Mar 23, 2023 · 5 comments
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?

Comments

@st-pasha
Copy link

Describe the style change

If a function takes a single argument which is a list, set, dict, or string, then the delimiters for those expressions can be immediately adjacent to the function's parentheses.

This would save both horizontal and vertical space, while also improving readability.

Examples in the current Black style

foo(
    [
        100,
        200,
        300,
    ]
)
bar(
    {
        "alpha": 1,
        "beta": 2,
        "gamma": 3,
    }
)
baz(
    """
    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed doeiusmod tempor incididunt
"""
)

Desired style

foo([
    100,
    200,
    300,
])
bar({
    "alpha": 1,
    "beta": 2,
    "gamma": 3,
})
baz("""
    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed doeiusmod tempor incididunt
""")

Additional context

@st-pasha st-pasha added the T: style What do we want Blackened code to look like? label Mar 23, 2023
@Jackenmen
Copy link
Contributor

Multiline string handling has been improved in #1879 in the way you've presented in the Desired style. This is currently available in the preview style on the main branch and will be able under that flag once a new version is released.

I don't think the rest of the proposed changes fit with Black's intended style and it wouldn't quite follow the goal of minimizing diffs as much if another argument were passed to the function in the future. But I can't speak for maintainers so maybe I'm completely wrong here.

@JelleZijlstra JelleZijlstra added the F: linebreak How should we split up lines? label Mar 24, 2023
@st-pasha
Copy link
Author

it wouldn't quite follow the goal of minimizing diffs as much if another argument were passed to the function in the future.

This is something that the developer can control using a trailing comma. If I have a single-argument function and I put a comma after the argument, then it means I anticipate that there could be more arguments in the future, and I want each of them to be on its own line. However, if I don't add a comma, then it means I don't think there will be new arguments for my function, or even if there would be, I'd prefer them to be in one line if possible.

@st-pasha
Copy link
Author

st-pasha commented Apr 3, 2023

In yapf this setting is called COALESCE_BRACKETS.

@rdaysky
Copy link

rdaysky commented Apr 24, 2023

Duplicate of #1811?

@st-pasha
Copy link
Author

Yeah, looks like it. Sorry for posting a duplicate.

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? T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

4 participants