5
votes

According to guido (and to some other Python programmers), implicit string literal concatenation is considered harmful. Thus, I am trying to identifying logical lines containing such a concatenation.

My first (and only) attempt was using shlex; I thought of splitting a logical line with posix=False, so I'll identify parts encapsulated by quotes, and if these lie next to each other, it will be considered "literal concatenation".

However, this fails on multiline strings, as the following example shows:

shlex.split('""" Some docstring """', posix=False)
# Returns '['""', '" Some docstring "', '""']', which is considered harmful, but it's not

I can tweak this is some weird ad-hoc ways, but I wondered whether you can think of a simple solution for this. My intention is to add it to my already extended pep8 verifier.

3
Tokenize the source with tokenize and examine the token stream? - user2357112 supports Monica
+1 for the interesting thread. I for one use implicit string concatenation all the time to continue lines and I hate the thought of having to throw a bunch of ugly + in there ... But, if it happens, I suppose I'll deal with it. - mgilson
Tokenizing is a great idea. - Bach

3 Answers

3
votes

Interesting question, I just had to play with it and because there is no answer I'm posting my solution to the problem:

#!/usr/bin/python

import tokenize
import token
import sys

with open(sys.argv[1], 'rU') as f:
    toks = list(tokenize.generate_tokens(f.readline))
    for i in xrange(len(toks) - 1):
        tok = toks[i]
        # print tok
        tok2 = toks[i + 1]
        if tok[0] == token.STRING and tok[0] == tok2[0]:
            print "implicit concatenation in line " \
                "{} between {} and {}".format(tok[2][0], tok[1], tok2[1])

You can feed the program with itself and the result should be

implicit concatenation in line 14 between "implicit concatenation in line " and "{} between {} and {}"
2
votes

I've decided to use the advice from user2357112, and extend it a bit to derive the following solution, which I describe here as an extension to the pep8 module:

def python_illegal_concetenation(logical_line):
    """
    A language design mistake from the early days of Python.
    https://mail.python.org/pipermail/python-ideas/2013-May/020527.html

    Okay: val = "a" + "b"
    W610: val = "a" "b"
    """
    w = "W610 implicit string literal concatenation considered harmful"
    sio = StringIO.StringIO(logical_line)
    tgen = tokenize.generate_tokens(sio.readline)
    state = None
    for token_type, _, (_, pos), _, _ in tgen:
      if token_type == tokenize.STRING:
        if state == tokenize.STRING:
          yield pos, w
        else:
          state = tokenize.STRING
      else:
        state = None
0
votes

One idea to deal with this better, is to put a space (or two) AFTER the close quote when you have a list:

aList = [
   'one'  ,
   'two'  ,
   'three'
   'four'  ,
]

Now it's more obvious that 'three' is missing its trailing comma

PROPOSAL: I suggest python have a pragma that indicates string literal concatenation is forbidden in a region:

@nostringliteralconcat
a = "this" "and" "that"   # Would cause a compiler failure
@stringliteralconcat
a = "this" "and" "that"   # Successfully Compiles

Allowing the concatenation would be the default (to maintain compatibility)

There is also this thread:

https://groups.google.com/forum/#!topic/python-ideas/jP1YtlyJqxs%5B1-25%5D