black icon indicating copy to clipboard operation
black copied to clipboard

Running black under Python versions 3.10.15 and 3.10.16 yields different results for parentheses inside case statements

Open jsolbrig opened this issue 1 year ago • 1 comments

Describe the bug

There is a formatting difference depending on whether you have Python <= 3.10.15 or Python > 3.10.15 installed but with the same version of Black (currently 25.1.0).

Under 3.10.15 this gets reformatted:

match args:
    case _ if ("test" in "testing" and True is True):
        print("YAY")

to this:

match args:
    case _ if "test" in "testing" and True is True:
        print("YAY")

Under 3.10.16 this reformatting doesn't occur and the parentheses remain in place.

The two statements functionally identical so I don't see why it would be reformatted for one version of Python and not for another.

To Reproduce

For example, take this code and call black on it with Python 3.10.15 and 3.10.16 installed. The results will be different.

match args:
    case _ if ("test" in "testing" and True is True):
        print("YAY")

And run it with these arguments:

$ black file.py
  • Black's version: 25.1.0
  • OS and Python version: Linux - Python 3.10.15 and 3.10.16 yield different results

Additional context

jsolbrig avatar Jan 30 '25 23:01 jsolbrig

I can't reproduce this:

$ ~/.pyenv/versions/3.10.15/bin/python -m black --diff /tmp/issue4569.py 
--- /tmp/issue4569.py	2025-01-31 01:25:45.750911+00:00
+++ /tmp/issue4569.py	2025-01-31 02:05:05.187473+00:00
@@ -1,3 +1,3 @@
 match args:
-    case _ if ("test" in "testing" and True is True):
+    case _ if "test" in "testing" and True is True:
         print("YAY")
would reformat /tmp/issue4569.py

All done! ✨ 🍰 ✨
1 file would be reformatted.
$ ~/.pyenv/versions/3.10.16/bin/python -m black --diff /tmp/issue4569.py 
--- /tmp/issue4569.py	2025-01-31 01:25:45.750911+00:00
+++ /tmp/issue4569.py	2025-01-31 02:05:12.407993+00:00
@@ -1,3 +1,3 @@
 match args:
-    case _ if ("test" in "testing" and True is True):
+    case _ if "test" in "testing" and True is True:
         print("YAY")
would reformat /tmp/issue4569.py

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

JelleZijlstra avatar Jan 31 '25 02:01 JelleZijlstra

I can't either.

$ cat repro/temp.py 
match args:
    case _ if ("test" in "testing" and True is True):
        print("YAY")

$ ~/.pyenv/versions/3.10.16/bin/python -m black --version
python -m black, 25.1.0 (compiled: yes)
Python (CPython) 3.10.16

$ ~/.pyenv/versions/3.10.15/bin/python -m black --version
python -m black, 25.1.0 (compiled: yes)
Python (CPython) 3.10.15

$ ~/.pyenv/versions/3.10.16/bin/python -m black --diff repro/temp.py 
--- repro/temp.py	2025-08-07 08:21:40.107027+00:00
+++ repro/temp.py	2025-08-07 08:24:13.724780+00:00
@@ -1,3 +1,3 @@
 match args:
-    case _ if ("test" in "testing" and True is True):
+    case _ if "test" in "testing" and True is True:
         print("YAY")
would reformat repro/temp.py

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

$ ~/.pyenv/versions/3.10.15/bin/python -m black --diff repro/temp.py 
--- repro/temp.py	2025-08-07 08:21:40.107027+00:00
+++ repro/temp.py	2025-08-07 08:24:24.633586+00:00
@@ -1,3 +1,3 @@
 match args:
-    case _ if ("test" in "testing" and True is True):
+    case _ if "test" in "testing" and True is True:
         print("YAY")
would reformat repro/temp.py

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

ranjodhsingh1729 avatar Aug 07 '25 08:08 ranjodhsingh1729