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

Opinion: Unwanted indentation of multiline string #233

Closed
KangOl opened this issue May 19, 2018 · 3 comments
Closed

Opinion: Unwanted indentation of multiline string #233

KangOl opened this issue May 19, 2018 · 3 comments

Comments

@KangOl
Copy link

KangOl commented May 19, 2018

I tend to write my SQL queries using this formatting.

def get_stuff(cr, value):
    cr.execute("""
        SELECT whatever
          FROM some_table t
         WHERE id = %s
    """, [value])
    return cr.fetchone()

However, black --diff produce the following output

--- foo.py  (original)
+++ foo.py  (formatted)
@@ -1,8 +1,11 @@
 def get_stuff(cr, value):
-    cr.execute("""
+    cr.execute(
+        """
         SELECT whatever
           FROM some_table t
          WHERE id = %s
-    """, [value])
+    """,
+        [value],
+    )
     return cr.fetchone()

reformatted foo.py

Which I find less readable, unpleasant.

Shouldn't black keep a multiline string first argument on the same line? (unless the line is too long, obviously)

@JelleZijlstra
Copy link
Collaborator

I encountered the same issue with calls to re.compile("""really long multiline regex""", re.VERBOSE)

@ambv
Copy link
Collaborator

ambv commented May 20, 2018

No. Black never hugs arguments to parentheses. Think of the opening triple quotes like an opening bracket. Black wouldn't keep it on the same line with another opening bracket.

Instead of using this formatting:

def get_stuff(cr, value):
    cr.execute("""
        SELECT whatever
          FROM some_table t
         WHERE id = %s
    """, [value])
    return cr.fetchone()

Black considers this a better formatting:

def get_stuff(cr, value):
    cr.execute(
        """
        SELECT whatever
        FROM some_table t
        WHERE id = %s
        """,
        [value],
    )
    return cr.fetchone()

This is symmetrical to something like:

def get_stuff(cr, value):
    cr.execute(
        (
            'SELECT whatever '
            'FROM some_table t '
            'WHERE id = %s'
        ),
        [value],
    )
    return cr.fetchone()

Unfortunately it's not safe for Black to change indentation within your multiline strings, so that it up to you.

@ambv
Copy link
Collaborator

ambv commented May 29, 2018

There are cases of multiline string support that need improvements (like #256 or #232) but this doesn't seem actionable to me. Closing.

@ambv ambv closed this as completed May 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants