Skip to content

ESP: a standalone comment causes strings to be merged into one far too long (and requiring two passes to do so) #2734

@ichard26

Description

@ichard26

To Reproduce

For example, take this code:

def a():
    CompiledSQL(
        "addresses.id AS addresses_id, "
        "addresses.email_address AS addresses_email_address "
        # note the comma-separated FROM clause
        "IN (__[POSTCOMPILE_primary_keys]) "
        "AND addresses.id = anon_1.id ORDER BY addresses.id",
        ["hmm"]
    )

And run it with these arguments:

$ black file.py --experimental-string-processing

The resulting error is:

error: cannot format test.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /tmp/blk_wqp6guhd.log
Oh no! 💥 💔 💥
1 file failed to reformat.
Mode(target_versions={<TargetVersion.PY36: 6>, <TargetVersion.PY38: 8>, <TargetVersion.PY37: 7>}, line_length=88, string_normalization=True, is_pyi=False, is_ipynb=False, magic_trailing_comma=True, experimental_string_processing=True)
--- source
+++ first pass
@@ -1,9 +1,9 @@
 def a():
     CompiledSQL(
         "addresses.id AS addresses_id, "
         "addresses.email_address AS addresses_email_address "
         # note the comma-separated FROM clause
-        "IN (__[POSTCOMPILE_primary_keys]) "
-        "AND addresses.id = anon_1.id ORDER BY addresses.id",
-        ["hmm"]
+        "IN (__[POSTCOMPILE_primary_keys]) AND addresses.id = anon_1.id ORDER BY"
+        " addresses.id",
+        ["hmm"],
     )
--- first pass
+++ second pass
@@ -1,9 +1,8 @@
 def a():
     CompiledSQL(
         "addresses.id AS addresses_id, "
         "addresses.email_address AS addresses_email_address "
         # note the comma-separated FROM clause
-        "IN (__[POSTCOMPILE_primary_keys]) AND addresses.id = anon_1.id ORDER BY"
-        " addresses.id",
+        "IN (__[POSTCOMPILE_primary_keys]) AND addresses.id = anon_1.id ORDER BY addresses.id",
         ["hmm"],
     )

Expected behavior

It not crashing 🙂

Environment

  • Black's version: main on b8df7e4
  • OS and Python version: Ubuntu 20.04.03 LTS w/ CPython 3.8.5

Additional context

This was discovered by primer (in sqlalchemy, test/orm/test_relationship_criteria.py) and I wasn't able to fix it quickly so instead of letting primer fail on every build unproductively, I'm excluding it and filing this issue so we don't forget about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: unstable formattingFormatting changed on the second passF: linetoolongBlack makes our lines too longF: stringsRelated to our handling of stringsT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions