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

Black doesn't wrap long line but wraps shorter line #2548

Closed
DanInSpace104 opened this issue Oct 20, 2021 · 2 comments
Closed

Black doesn't wrap long line but wraps shorter line #2548

DanInSpace104 opened this issue Oct 20, 2021 · 2 comments
Labels
F: linebreak How should we split up lines? F: linetoolong Black makes our lines too long F: strings Related to our handling of strings T: bug Something isn't working

Comments

@DanInSpace104
Copy link

DanInSpace104 commented Oct 20, 2021

Describe the bug

Black fixes line with length of 120 but doesn't fix line with length of 152. When i tried to fix it myself Black reverted my changes.

To Reproduce

  1. Take this code:
def temp():
    datetime_start_result = f'{datetime_start[6:10]}-{datetime_start[3:5]}-{datetime_start[0:2]} {datetime_start[11:]}'
    datetime_finish_result = f'{datetime_finish[6:10]}-{datetime_finish[3:5]}-{datetime_finish[0:2]} {datetime_finish[11:]}'
  1. Run Black on it with these arguments -l 100 -S
  2. First line with length 120 was fixed but second line with length 125 wasn't
def temp():
    datetime_start_result = (
        f'{datetime_start[6:10]}-{datetime_start[3:5]}-{datetime_start[0:2]} {datetime_start[11:]}'
    )
    datetime_finish_result = f'{datetime_finish[6:10]}-{datetime_finish[3:5]}-{datetime_finish[0:2]} {datetime_finish[11:]}'

Expected behavior
Black should format this code as:

def temp():
    datetime_start_result = (
        f'{datetime_start[6:10]}-{datetime_start[3:5]}-{datetime_start[0:2]} {datetime_start[11:]}'
    )
    datetime_finish_result = (
        f'{datetime_finish[6:10]}-{datetime_finish[3:5]}-{datetime_finish[0:2]} {datetime_finish[11:]}'
    )

Environment (please complete the following information):

  • Version: 21.9b0
  • OS and Python version: Linux/Python 3.9.5

Does this bug also happen on main?
Yes.

Additional context

When you format this code Black wraps both lines correct:

datetime_start_result = f'{datetime_start[6:10]}-{datetime_start[3:5]}-{datetime_start[0:2]} {datetime_start[11:]}'
datetime_finish_result = f'{datetime_finish[6:10]}-{datetime_finish[3:5]}-{datetime_finish[0:2]} {datetime_finish[11:]}'

I think this happens because black cannot split f-string into parts and so it can not make line shorter than 100 symbols, so it leaves this line unchanged. But in that case, Black should NOT discard my manual changes to this line and make it longer, or black should try to make line as short as possible even when it still longer than --line-length

@DanInSpace104 DanInSpace104 added the T: bug Something isn't working label Oct 20, 2021
@vbarbaresi
Copy link
Contributor

There is an experimental flag --experimental-string-processing that solves this issue.
It will become the default behavior in the future #2188

@ichard26 ichard26 added F: linebreak How should we split up lines? F: linetoolong Black makes our lines too long F: strings Related to our handling of strings labels Oct 24, 2021
@DanInSpace104
Copy link
Author

There is an experimental flag --experimental-string-processing

Thank you, now it works just as I expected!

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? F: linetoolong Black makes our lines too long F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants