black icon indicating copy to clipboard operation
black copied to clipboard

Black producing very different output for the same file.

Open boatcoder opened this issue 3 years ago • 3 comments

Describe the bug Using black on 2 different branches of the same code (but with the same config files) I'm getting very different output from black

diff --git a/tasks.py b/tasks.py
index e1303c3..f3f626a 100644
--- a/tasks.py
+++ b/tasks.py
@@ -4,9 +4,7 @@ def scrape_one_twitter(sa_id: int, attempt: int):
     impact the rest. Also these are spaced out time to mitigate the rate limiting"""
     try:
         client = tweepy.Client(
-            bearer_token=os.environ["TWITTER_BEARER_TOKEN"],
-            wait_on_rate_limit=True,
-            return_type=dict,
+            bearer_token=os.environ["TWITTER_BEARER_TOKEN"], wait_on_rate_limit=True, return_type=dict
         )

         social_account = SocialAccount.objects.select_related("user", "user__profile").get(pk=sa_id)

I have run rm -rf ~/Library/Caches/black/* in between the branch switches just to see if that was having any effect and it did not.

We've seen this on another machine where his black formatted code was not the same as mine, but this is using one VENV on one machine with one REPO and 2 branches.

I thought it must be a configuration problem until I can check out the different version from branch B, and it doesn't reformat it which says that Black is perfectly happy with 2 different formats of the exact same file.

Expected behavior

black would always format the same code the same way.

  • Black's version: black, 22.1.0 (compiled: yes)
  • OS and Python version: MacOS 12.2.1 / Python 3.10.2

Additional context

I've cut the repo down to 1 file with 2 branches and put a script in the README.md that reproduces this problem. requirements.txt contains 1 requirement (black)

https://github.com/boatcoder/black-issue

boatcoder avatar Apr 26 '22 23:04 boatcoder

Some additional info: skip-magic-trailing-comma = true seems to move this problem to another spot

-                    cn.mobile_country_code, created = MobileCountryCode.objects.get_or_create(
+                    (cn.mobile_country_code, created) = MobileCountryCode.objects.get_or_create(
                         country_code=str(info["carrier"]["mobile_country_code"]).lower()
                     )
-                    cn.mobile_network_code, created = MobileNetworkCode.objects.get_or_create(
+                    (cn.mobile_network_code, created) = MobileNetworkCode.objects.get_or_create(
                         network_code=str(info["carrier"]["mobile_network_code"]).lower()
                     )

Same problem that one branch has one formatting and one branch has the other with the same configuration.

boatcoder avatar Apr 27 '22 03:04 boatcoder

Black doesn't promise to format all equivalent code the exact same way. Black's behaviour does depend on the previous formatting (although Black tries to avoid doing so). Magic trailing comma is an obvious example of where Black takes previous formatting into account, but there are others, often involving parentheses.

hauntsaninja avatar Apr 27 '22 06:04 hauntsaninja

It introduced those parens, I didn't write it that way but that probably happened with the magic parens. The fact that it. "sometimes" reformats things so that there is nothing deterministic about it means that diffs can get really painful (like last night).

I think claims like this should come with a serious asterisk next to them:

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you're reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

Except when it doesn't.....

boatcoder avatar Apr 27 '22 18:04 boatcoder

I don't see an actionable issue here. #2135 already proposes removing the magic trailing comma by default.

JelleZijlstra avatar Dec 20 '22 02:12 JelleZijlstra