psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
39.11k stars 2.47k forks source link

Black fails to format string which has '='s etc #4361

Closed rushiagr closed 6 months ago

rushiagr commented 6 months ago

Describe the bug

For a particular string in my source, it is not able to format.

To Reproduce Please see below output produced by black

--- src
+++ dst
@@ -81541,11 +81541,11 @@
             value=
             Constant(
                 kind=
                 None,  # NoneType
                 value=
-                "\n    ===============================================================================================\n    \n    # NOTE: when parquet generation script is stopped accidentally (some parquet files already generated, but not all), \n    #   and need to restart (worked on 13 May 2024):\n    \n    1. Delete all today's files in /tmp/quotefiles/[parquet|extracted]\n    2. Delete all today's files in s3 bucket rushitestbucket2/kitedata/pq10secquotes[|bn|fn|mn|sn]\n    3. Uncompress allquotes and allinstruments files back to /drive/kitedata:\n        cd /drive/kitedata/compressed/quotes\n        gunzip -c allquotes-2024-05-13  >  /drive/kitedata/allquotes-2024-05-13\n        cd /drive/kitedata/compressed/instruments\n        gunzip -c  allinstruments-2024-05-13  >  /drive/kitedata/allinstruments-2024-05-13\n    4. Run generate_all_parquet_files via automation (change time to be immediately after current time)\n    \n    # NOTE: below methods didn't work for some reason, in this particular case\n    \n    ==============================================================================================\n    \n    Command used to locally test midcpnifty and sensex parquet generation:\n    python squash.py --generate-parquet-files --dates-to-process 2023-08-03,2023-08-04 --index MIDCPNIFTY --skip-upload\n    python squash.py --generate-parquet-files --dates-to-process 2023-08-03,2023-08-04 --index SENSEX --skip-upload\n    ",  # str
+                "\n    ===============================================================================================\n\n    # NOTE: when parquet generation script is stopped accidentally (some parquet files already generated, but not all),\n    #   and need to restart (worked on 13 May 2024):\n\n    1. Delete all today's files in /tmp/quotefiles/[parquet|extracted]\n    2. Delete all today's files in s3 bucket rushitestbucket2/kitedata/pq10secquotes[|bn|fn|mn|sn]\n    3. Uncompress allquotes and allinstruments files back to /drive/kitedata:\n        cd /drive/kitedata/compressed/quotes\n        gunzip -c allquotes-2024-05-13  >  /drive/kitedata/allquotes-2024-05-13\n        cd /drive/kitedata/compressed/instruments\n        gunzip -c  allinstruments-2024-05-13  >  /drive/kitedata/allinstruments-2024-05-13\n    4. Run generate_all_parquet_files via automation (change time to be immediately after current time)\n\n    # NOTE: below methods didn't work for some reason, in this particular case\n\n    ==============================================================================================\n\n    Command used to locally test midcpnifty and sensex parquet generation:\n    python squash.py --generate-parquet-files --dates-to-process 2023-08-03,2023-08-04 --index MIDCPNIFTY --skip-upload\n    python squash.py --generate-parquet-files --dates-to-process 2023-08-03,2023-08-04 --index SENSEX --skip-upload\n    ",  # str
             )  # /Constant
         )  # /Expr
         Expr(
             value=
             Constant(

Error message:

error: cannot format /Users/a/b/c/d.py: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /var/folders/5j/cgflppss17d3hs6zb_hyyrzw0000gn/T/blk_ummt734r.log

Expected behavior

Black should be able to reformat file Environment

Additional context

Apologies as I'm not able to search duplicates in issue tracker, as i'm not well-versed with black and the bugs here..

rushiagr commented 6 months ago

OK looks like the issue is with (trailing) whitespaces. I removed trailing whitespaces from the code, and black was able to format. Providing before (buggy) and after (black works):

Before: https://gist.github.com/rushiagr/78b01515286ee86696e3209970b73a3c

After: https://gist.github.com/rushiagr/437fef5046a4436673813e4cd6e8c992

I just redacted s3 bucket name to 'mys3bucket', else the code snippet was lifted as-is

hauntsaninja commented 6 months ago

Thanks, I don't quite follow your example, but I believe it should be fixed in Black 24.4.0 and newer (by https://github.com/psf/black/pull/4290). Please re-open if not!