Skip to content

make line wrapping better for inline if statements with comments in function args #1187

@ntextreme3

Description

@ntextreme3

I looked through some issues and didn't see any like this. Sorry if this is a duplicate or already discussed. I noticed something I thought was strange and probably the first thing I didn't like about black formatted code 😄

Something like this:

some_kind_of_data = "foo"
some_other_kind_of_data = "bar"
print(
    dict(
        a=1,
        b=2 if some_kind_of_data is not None else some_other_kind_of_data,  # some explanation of why this is actually necessary
        c=3,
    )
)

Using black (python3.7.3, black 19.10b0), becomes:

some_kind_of_data = "foo"
some_other_kind_of_data = "bar"
print(
    dict(
        a=1,
        b=2
        if some_kind_of_data is not None 
        else some_other_kind_of_data,  # some explanation of why this is actually necessary
        c=3,
    )
)

Desired style How do you think Black should format the above snippets:

Either leave it obnoxiously long, or add parenthesis / indentation

some_kind_of_data = "foo"
some_other_kind_of_data = "bar"
print(
    dict(
        a=1,
        b=(
            2 if some_kind_of_data is not None else some_other_kind_of_data
        ),  # some explanation of why this is actually necessary
        c=3,
    )
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: linebreakHow should we split up lines?T: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions