[Python-ideas] Descouraging the implicit string concatenation

Carl Meyer carl at oddbird.net
Wed Mar 14 12:53:42 EDT 2018


On 3/14/18 8:03 AM, Guido van Rossum wrote:
> I use the feature regularly for long error messages, and when combined
> with .format() or % it's not so easy to replace it with a + (it would
> require adding parentheses).
> 
> So I am against formally discouraging it or adding something to PEP 8.
> 
> However linters could warn about lists of comma-separated strings with a
> missing comma.

+1 to all of this.

It's possible to write a linter rule that will reliably catch the
potential missing-comma errors, while still allowing implicit string
concatenation where it's useful and readable. The rule is that implicit
string concatenation should only be disallowed if there are any commas
present at the same "parenthesization level". Thus, this is allowed:

    raise SomeException(
        "This is a really long exception message "
        "which I've split over two lines."
    )

While both of these are disallowed:

    fruits = [
        'orange',
        'grape'
        'banana',
    ]

    some_str.replace(
        "This is a long string "
        "split over two lines.",
        "And this is another string."
    )

For the latter case, you'd need to wrap the multiline string in its own
parens, use +, and/or previously assign it to its own variable. (I don't
think this should be controversial; the example is just asking for trouble.)

With this rule the only missing-comma that can slip through is when
you've forgotten _all_ the intervening commas in your sequence of
strings. That's much less likely.

Carl



More information about the Python-ideas mailing list