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

string_processing: Extra parentheses added even when string isn't split #3466

Open
wookie184 opened this issue Dec 20, 2022 · 2 comments
Open
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings T: bug Something isn't working

Comments

@wookie184
Copy link

Describe the bug
With preview style, a long f-string where the only spaces are in the expression bit has parentheses added even though it isn't split over lines. For example:

f(
    f'longstringwithnospacesinthestringbitbutwithspacesintheformattyfstringbithere{ 1 }',
)

becomes

f(
    (
        f"longstringwithnospacesinthestringbitbutwithspacesintheformattyfstringbithere{ 1 }"
    ),
)

I would instead expect it not to have changed. See on black playground

I noticed it also seems to occur with normal strings where the split is very near the start or end

f(
    "l ongstringwithnospacesinthestringbitbutwithspacesintheformattyfstringbitherelollol",
)

becomes

f(
    (
        "l ongstringwithnospacesinthestringbitbutwithspacesintheformattyfstringbitherelollol"
    ),
)

See on black playground
Version
Black 23.1a1, with --preview

@wookie184 wookie184 added the T: bug Something isn't working label Dec 20, 2022
@JelleZijlstra JelleZijlstra added F: strings Related to our handling of strings C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. labels Dec 20, 2022
@ownchoice
Copy link

I'm not sure if it's exactly the same problem but looks like it is, so I'm gonna share my case here instead of opening a new issue.

Describe the bug

Black turns some variable definition with strings as value into multiline assignments and adds parentheses, but doesn't seem to follow a pattern for doing so. Doesn't happen with some longer strings or variables names.

To Reproduce

Using this input:

test1: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/{animal_id}"

test2: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}"

test3 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}"

test4 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/longer/{animal_id}"

test5: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/something/else/{animal_id}"

test6: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/test"

test7 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/test"

test7 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/test2"

test8: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/lorem"

Format it with Black and you'll get the following output:

test1: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/{animal_id}"

test2: str = (
    "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}"
)

test3 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}"

test4 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/longer/{animal_id}"

test5: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/something/else/{animal_id}"

test6: str = (
    "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/test"
)

test7 = (
    "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/test"
)

test7 = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/test2"

test8: str = "https://{subdomain}.api.server.com/media/animals/v4/photos/animal/{animal_id}/lorem"

What I expected to happen

I can't figure out why would Black randomly use multiline strings when it seems unnecesary.

It's not because of line length.

Black doesn't add parentheses to longer string and/or longer variable names, so I'm at a loss about what is causing it to only happen with some strings.

May be related to issue #232

Things I have done already

  1. Updated Black's version to the newest release (pip install -U black)
  2. Tested this bug using the online formatter at https://black.vercel.app/?version=main
  3. Tested this bug using the online formatter at https://black.vercel.app/?version=stable
  4. Tested using a virtualenv with the lastest Black version on it

Environment

Env 1:

Env 2:

  • Black's version: 22.12.0 (latest)
  • Python version: Python 3.11.0
  • OS version: Windows 11 Pro 10.0.22621 Build 22621

@psf psf deleted a comment from ownchoice Dec 31, 2022
@ichard26
Copy link
Collaborator

ichard26 commented Dec 31, 2022

@ownchoice this is a different issue. Black doesn't wrap long string assignments if it would still violate the line length limit even after being exploded AFAIK. Maybe we should for consistency...

@ichard26 ichard26 added the F: parentheses Too many parentheses, not enough parentheses, and so on. label Dec 31, 2022
@JelleZijlstra JelleZijlstra changed the title Extra parentheses added even when string isn't split string_processing: Extra parentheses added even when string isn't split Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants