Black is not consistent wrt trailing commas in argument lists

glandium created this issue on 2021-07-23 · The issue is replied 1 times

Describe the bug

Running black before and after modifying a file doesn't produce the same formatting wrt trailing commas in argument lists.

To Reproduce

  1. Take this file https://hg.mozilla.org/mozilla-central/file/287995cabe8ebd9a29f2f56dde65a84678a38229/testing/mach_commands.py
  2. Run Black on it, it makes no change (double checked after an upgrade to 21.7b0).
  3. Apply the following patch:
@@ -683,7 +683,10 @@ class SpiderMonkeyTests(MachCommandBase):
         test_env = os.environ.copy()
         test_env["TOPSRCDIR"] = command_context.topsrcdir
 
-        return subprocess.call(jsapi_tests_cmd, env=test_env)
+        result = subprocess.call(jsapi_tests_cmd, env=test_env)
+        if result != 0:
+            print(f"jsapi-tests failed, exit code {result}")
+        return result
 
     def run_check_js_msg(self, command_context):
         import subprocess
  1. Run Black again.

Expected behavior

No change the second time. Maybe changes the first time.

Actual behavior

Applies these changes to the file

@@ -176,7 +176,7 @@ class AddTest(MachCommandBase):
         doc=None,
         overwrite=False,
         editor=MISSING_ARG,
-        **kwargs
+        **kwargs,
     ):
         import addtest
         import io
@@ -437,7 +437,7 @@ class Test(MachCommandBase):
                     suite["mach_command"],
                     command_context._mach_context,
                     argv=extra_args,
-                    **kwargs
+                    **kwargs,
                 )
                 if res:
                     status = res
@@ -464,7 +464,7 @@ class Test(MachCommandBase):
                 command_context._mach_context,
                 argv=extra_args,
                 test_objects=tests,
-                **kwargs
+                **kwargs,
             )
             if res:
                 status = res

Bonus

If you unapply the patch and run black again, you don't get back the original file, you get the original file with the kwargs changes applied.

Environment (please complete the following information):

  • Version: 21.7b0
  • OS and Python version: Linux/Python 3.7.3

Does this bug also happen on main?

Yes.

JelleZijlstra wrote this answer on 2021-07-23

This is because of how we autodetect whether a file is Python 3.6-only. When you add an f-string, we detect that the file is Python 3.6+ and therefore make changes that are only safe in 3.6+, such as adding the trailing space.

To avoid getting this sort of change when you add an f-string, add an explicit target_version in your configuration.

More Details About Repo
Owner Name psf
Repo Name black
Full Name psf/black
Language Python
Created Date 2018-03-14
Updated Date 2021-11-24
Star Count 23701
Watcher Count 197
Fork Count 1527
Issue Count 354

YOU MAY BE INTERESTED

Issue Title State Comments Created Date Updated Date Closed Date
[BUG] -Clean Regressions pod mount needs to be updated to `/joyvan/shared` closed 0 2021-10-11 2021-11-07 2021-10-27
渲染块大小调整,不进行语法错误识别 closed 0 2021-11-23 2021-11-17 2021-11-23
ImageMagick closed 2 2017-07-07 2021-11-01 2017-11-21
One question open 1 2019-11-19 2021-11-18 -
Exports is not defined in ES module scope closed 12 2021-10-22 2021-11-24 2021-11-02
[功能请求]忽略框架及依赖的版本 closed 4 2019-01-22 2021-11-26 2019-02-03
Github Page Error: Page Build Failed closed 0 2021-07-25 2021-11-14 2021-07-25
Make resume-template remote-theme compatible. open 1 2021-01-17 2021-11-13 -
Cluster does not rotate when user rotate the map open 0 2021-04-19 2021-11-01 -
Bug: $db->insertID() causes error after upgrading 4.1.5 closed 5 2021-11-15 2021-11-14 2021-11-16