Skip to content

Poor handling of multi-line string literals using slash continuation #1183

@peterjc

Description

@peterjc

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Make a small Python file containing the following:
if True:
    # Pointless if-statement just for indentation here:
    print("\
    \nMESSAGE - printing a multi-line message to the user here as a single\
    \nlong string literal taking multiple lines of Python source code.")
  1. Run Black on it with default arguments
  2. Observer the following output:
if True:
    # Pointless if-statement just for indentation here:
    print(
        "\
    \nMESSAGE - printing a multi-line message to the user here as a single\
    \nlong string literal taking multiple lines of Python source code."
    )

I find this apparently mixed indentation very ugly, but it works and may be the least bad solution without removing the slash line continuation.

Expected behavior A clear and concise description of what you expected to happen.

The ideal output would be something like this, but would require black to break up string literals (already covered on separate issues like #182):

if True:
    # Pointless if-statement just for indentation here:
    print(
        "\nMESSAGE - printing a multi-line message to the user here as a single"
        "\nlong string literal taking multiple lines of Python source code."
    )

A possible compromise would be remove the slash continuation giving a long string:

if True:
    # Pointless if-statement just for indentation here:
    print(
        "\nMESSAGE - printing a multi-line message to the user here as a single\nlong string literal taking multiple lines of Python source code."
    )

The output is not ideal, but would be easy to spot with linters, and the behaviour is simple to explain.

Environment (please complete the following information):

  • Version: 19.10b0 according to pip (although --version reports 19.3b0)
  • OS and Python version: macOS, Python 3.7.4 (conda)

Same on master checked with https://black.now.sh/?version=master

Additional context

This example is based on some real code https://github.com/biopython/biopython/blob/biopython-175/Scripts/Restriction/ranacompiler.py#L279 originally written in 2004.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions