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 producing very different output for the same file. #3037

Closed
boatcoder opened this issue Apr 26, 2022 · 4 comments
Closed

Black producing very different output for the same file. #3037

boatcoder opened this issue Apr 26, 2022 · 4 comments
Labels
T: bug Something isn't working

Comments

@boatcoder
Copy link

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 boatcoder added the T: bug Something isn't working label Apr 26, 2022
@boatcoder
Copy link
Author

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.

@hauntsaninja
Copy link
Collaborator

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.

@boatcoder
Copy link
Author

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.....

@JelleZijlstra
Copy link
Collaborator

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

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2022
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

3 participants