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

Line too long: Long triple-quoted string #1617

Open
brettcannon opened this issue Aug 22, 2020 · 5 comments
Open

Line too long: Long triple-quoted string #1617

brettcannon opened this issue Aug 22, 2020 · 5 comments
Labels
F: linetoolong Black makes our lines too long F: strings Related to our handling of strings T: style What do we want Blackened code to look like?

Comments

@brettcannon
Copy link
Member

Describe the bug Adding an = within an f-string that is too long to fit on a line causes Black to undo the work it did to try and make the line fit within the column restrictions

To Reproduce Steps to reproduce the behavior:

  1. Take this code:
class Wraps:
    binary_inplace_op.__doc__ = f"""Implement the augmented arithmetic assignment `a {binary_op.operator} b`."""


class NoWrap:
    binary_inplace_op.__doc__ = f"""Implement the augmented arithmetic assignment `a {binary_op.operator}= b`."""
  1. Run Black
  2. Notice how the line in the Wraps class gets wrapped to try and make the line shorter, while in the NoWrap class the introduction of a = in the f-string undoes the wrapping.

Expected behavior
To see the line with the = in the f-string` still be wrapped since it is now even longer than before.

Environment (please complete the following information):

  • Version: 19.10b0
  • OS and Python version: CPython 3.8.3, macOS

Does this bug also happen on master?

Yes.

@brettcannon brettcannon added the T: bug Something isn't working label Aug 22, 2020
@zsol
Copy link
Collaborator

zsol commented Aug 22, 2020

FWIW I don't think that = character is special, it has to do with the length of the string I think: example.

In the original example in OP, the string in Wraps is 73 characters long. + 6 " + 1 f makes it 80, and after indenting two levels it's exactly 88 characters long. NoWrap adds one more character, which makes it impossible to keep the line length under 88, so Black just doesn't even try indenting.

I think this is working as intended. We can have a discussion if this is the right intention or not :)

@zsol zsol added T: style What do we want Blackened code to look like? and removed T: bug Something isn't working labels Aug 22, 2020
@JelleZijlstra
Copy link
Collaborator

Does it work with --experimental-string-processing?

@brettcannon
Copy link
Member Author

@JelleZijlstra same results.

@ambv
Copy link
Collaborator

ambv commented Aug 26, 2020

Yeah, this is not a bug. There's no point putting additional parentheses around things that won't fit anyway. If you split the string into two, you'll see correct behavior. It's a bug that --experimental-string-processing doesn't do this automatically for you though.

@JelleZijlstra JelleZijlstra added F: linetoolong Black makes our lines too long F: strings Related to our handling of strings labels May 30, 2021
@bbugyi200
Copy link
Contributor

bbugyi200 commented Jun 5, 2021

@JelleZijlstra @ambv I know I'm late to the party here, but note that --experimental-string-processing (i.e. #1132) does not attempt to improve (or change at all) the way black handles triple-quoted strings.

@JelleZijlstra JelleZijlstra changed the title Introducing a = in a string literal on a line that is too long undoes the attempted column shortening Line too long: Long triple-quoted string Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linetoolong Black makes our lines too long F: strings Related to our handling of strings T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

5 participants