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's treatment of trailing commas depends on previous statements. #4403

Closed
JohnADawson opened this issue Jul 13, 2024 · 2 comments
Closed
Labels
T: bug Something isn't working

Comments

@JohnADawson
Copy link

Describe the bug

Black's treatment of trailing commas depends on previous statements.

To Reproduce

If file0.py is

print(
    *[],
    "Once",
    "there",
    "were",
    "four",
    "children",
    "whose",
    "names",
    "were",
    "Peter,",
    "Susan,",
    "Edmund",
    "and",
    "Lucy."
)

running

black --diff file0.py

produces

All done! ✨ 🍰 ✨
1 file would be left unchanged.

If file1.py is

f""
print(
    *[],
    "Once",
    "there",
    "were",
    "four",
    "children",
    "whose",
    "names",
    "were",
    "Peter,",
    "Susan,",
    "Edmund",
    "and",
    "Lucy."
)

running

black --diff file1.py

produces

--- file1.py	2024-07-13 22:34:48.876534+00:00
+++ file1.py	2024-07-13 22:35:00.795624+00:00
@@ -11,7 +11,7 @@
     "were",
     "Peter,",
     "Susan,",
     "Edmund",
     "and",
-    "Lucy."
+    "Lucy.",
 )
would reformat file1.py

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

Expected behavior

The previous statement f"" does not affect the trailing comma after "Lucy.".

Environment

  • Black's version: 24.4.2
  • OS and Python version: Ubuntu 24.04, Python 3.12.3

The same behaviour is exhibited by https://black.vercel.app/?version=main.

@JohnADawson JohnADawson added the T: bug Something isn't working label Jul 13, 2024
@JelleZijlstra
Copy link
Collaborator

I believe this is because a trailing comma in this context is only legal in 3.6+. If you use an f-string, we know the file is 3.6+, so we can use other 3.6+ syntax.

However, Python 3.5 is long dead. We can probably get rid of this behavior now.

@JohnADawson
Copy link
Author

@JelleZijlstra, as your explanation predicts, running

black --diff --target-version=py312 file0.py 

produces

--- file0.py	2024-07-13 21:54:24.113436+00:00
+++ file0.py	2024-07-13 22:52:31.502265+00:00
@@ -10,7 +10,7 @@
     "were",
     "Peter,",
     "Susan,",
     "Edmund",
     "and",
-    "Lucy."
+    "Lucy.",
 )
would reformat file0.py

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants