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

Improved string processing: add parentheses around implicit string concatenations to increase readability in certain contexts #3159

Closed
yilei opened this issue Jul 6, 2022 · 1 comment · Fixed by #3162
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?

Comments

@yilei
Copy link
Contributor

yilei commented Jul 6, 2022

Describe the style change

When splitting long string literals in Black's --preview Improved string processing style, add parentheses around implicit string concatenations to increase readability in certain contexts.

"certain contexts" include:

  • Function parameters
  • Sequence elements

Examples of unformatted code

                    flash(
                        'None of the email addresses or domains you entered are valid',
                        'error',
                    )
some_list = [
    ' lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim',
    ' veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo',
]

Examples in the current Black style

                    flash(
                        'None of the email addresses or domains you entered'
                        ' are valid',
                        'error',
                    )
some_list = [
    " lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor"
    " incididunt ut labore et dolore magna aliqua Ut enim ad minim",
    " veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo",
]

Desired style

                    flash(
                        (
                            'None of the email addresses or domains you entered'
                            ' are valid'
                        ),
                        'error',
                    )
some_list = [
    (
        " lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor"
        " incididunt ut labore et dolore magna aliqua Ut enim ad minim"
    ),
    " veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo",
]

Additional context

This issue was raised originally in #2188 (comment). Per @JelleZijlstra, I'm starting a new issue separately here to track the work.

The "certain contexts" list isn't exhaustive. I plan to read the code and play around with an implementation, then report back here.

@yilei yilei added the T: style What do we want Blackened code to look like? label Jul 6, 2022
@ichard26 ichard26 added F: strings Related to our handling of strings S: needs discussion Needs further hashing out before ready for implementation (on desirability, feasibility, etc.) and removed S: needs discussion Needs further hashing out before ready for implementation (on desirability, feasibility, etc.) labels Jul 7, 2022
@m-aciek
Copy link
Contributor

m-aciek commented Jul 12, 2022

Alternative solution for the issue here, as suggested by @haridsv, is an indentation:

                    flash(
                        'None of the email addresses or domains you entered'
                            ' are valid',
                        'error',
                    )
some_list = [
    " lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor"
        " incididunt ut labore et dolore magna aliqua Ut enim ad minim",
    " veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo",
]

@ichard26 ichard26 added F: parentheses Too many parentheses, not enough parentheses, and so on. S: accepted The changes in this design / enhancement issue have been accepted and can be implemented labels Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants