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

Preview style unnecessarily moves comment #3924

Closed
JelleZijlstra opened this issue Oct 6, 2023 · 9 comments
Closed

Preview style unnecessarily moves comment #3924

JelleZijlstra opened this issue Oct 6, 2023 · 9 comments
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. F: strings Related to our handling of strings T: bug Something isn't working

Comments

@JelleZijlstra
Copy link
Collaborator

The preview style moves a comment on one line of an implicitly concatenated string to a different line.

$ python -m black --version
python -m black, 23.9.1 (compiled: yes)
Python (CPython) 3.9.14
$ python -m black --diff --preview movecomment.py 
--- movecomment.py      2023-10-06 01:40:10.153647+00:00
+++ movecomment.py      2023-10-06 01:40:41.077230+00:00
@@ -1,7 +1,7 @@
 xxxxxxx.xxfx(
-    f"xxxx_xxxxxxxx: "  # xxxxxxx
+    "xxxx_xxxxxxxx: "
     f"xxxxx={xxxx_xxxxxxxx.xxxxx}, "
     f"_xxx_xxxxxx={xxxx_xxxxxxxx._xxx_xxxxxx}, "
     f"xxx_xxxxxx={xxxx_xxxxxxxx.xxx_xxxxxx}, "
-    f"xxx_xxxxx={xxxx_xxxxxxxx.xxx_xxxxx}"
+    f"xxx_xxxxx={xxxx_xxxxxxxx.xxx_xxxxx}"  # xxxxxxx
 )
would reformat movecomment.py

All done! ✨ 🍰 ✨
1 file would be reformatted.
$ cat movecomment.py 
xxxxxxx.xxfx(
    f"xxxx_xxxxxxxx: "  # xxxxxxx
    f"xxxxx={xxxx_xxxxxxxx.xxxxx}, "
    f"_xxx_xxxxxx={xxxx_xxxxxxxx._xxx_xxxxxx}, "
    f"xxx_xxxxxx={xxxx_xxxxxxxx.xxx_xxxxxx}, "
    f"xxx_xxxxx={xxxx_xxxxxxxx.xxx_xxxxx}"
)
@JelleZijlstra JelleZijlstra added T: bug Something isn't working 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 Oct 6, 2023
@henriholopainen
Copy link
Contributor

This seems to be happening also with the stable style. Should it still be considered as a bug?

% black --version
black, 23.9.1 (compiled: yes)
Python (CPython) 3.11.3
% black --diff movecomment.py
--- movecomment.py	2023-10-14 08:29:58.442254+00:00
+++ movecomment.py	2023-10-14 08:29:59.965634+00:00
@@ -1,7 +1,7 @@
 xxxxxxx.xxfx(
-    f"xxxx_xxxxxxxx: "  # xxxxxxx
+    "xxxx_xxxxxxxx: "
     f"xxxxx={xxxx_xxxxxxxx.xxxxx}, "
     f"_xxx_xxxxxx={xxxx_xxxxxxxx._xxx_xxxxxx}, "
     f"xxx_xxxxxx={xxxx_xxxxxxxx.xxx_xxxxxx}, "
-    f"xxx_xxxxx={xxxx_xxxxxxxx.xxx_xxxxx}"
+    f"xxx_xxxxx={xxxx_xxxxxxxx.xxx_xxxxx}"  # xxxxxxx
 )
would reformat movecomment.py

All done! ✨ 🍰 ✨
1 file would be reformatted.

@JelleZijlstra
Copy link
Collaborator Author

It doesn't happen in the stable style. You may have a configuration file that sets preview to true.

@henriholopainen
Copy link
Contributor

Hmm, couldn't find the configuration, but a clean container install proves you are correct. I can try and take a look at this issue.

1 similar comment
@henriholopainen
Copy link
Contributor

Hmm, couldn't find the configuration, but a clean container install proves you are correct. I can try and take a look at this issue.

@JelleZijlstra
Copy link
Collaborator Author

black -v should print where it gets its config from.

@henriholopainen
Copy link
Contributor

This is proving to be a bit tricky. Touching implicitly concatenated strings with comments on any line could change the semantics of the comment. The safest approach would be to not transform such strings at all and hold onto the integrity of the comment. Currently the logic goes like this:

foo(
  "a"  # c1
  "b"  # c2
  "c"  # c3
)
-->
foo("abc")  # c1  # c2  # c3

I think we should either accept this, or then having a comment on any line "a" – "c" should skip processing for the whole string. Any thoughts?

@JelleZijlstra
Copy link
Collaborator Author

We probably should just keep your example code unchanged. If the user put a comment next to part of the string, we should keep it there.

Though note that we currently merge the comments if the different lines hold non-strings instead:

$ black -c 'foo(
> a, # 1
> b, # 2
> c  # 3
> )'
foo(a, b, c)  # 1  # 2  # 3

That's a behavior I'd be open to changing.

@henriholopainen
Copy link
Contributor

A fix proposal in this PR, let me know what you think! I can take a look at the non-string case in another PR if you think it is worth the time.

@JelleZijlstra
Copy link
Collaborator Author

Thanks for the fix!

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: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants