Check concatenated string literals in Python source code.
checkimpcont.py [file]
The example applies the script to its own source.
checkimpcont.py checkimpcont.py
It detects one warning:
41:43: warning: string literal concatenation
print("%s:%s: warning: string literal "
~~~^
python setup.py install
# -- or --
pip install .
Like C, Python allows adjacent string literals to be implicitly concatenated. In particular, this can happen to strings on different lines. Sometimes, this can be confusing when initializing a sequence, for instance,
L = ["spam",
"eggs"
"ham"]
This above list has two elements, "spam"
and "eggsham"
, but it might not be
obvious at first glance.
See also the StackOverflow posts:
- Issue warning for missing comma between list items bug
- Can I get a lint error on implicit string joining in python?
The implementation uses a pushdown machine to detect the STRING
tokens
followed by NL
tokens that should be picked out (not all of them are). A
pushdown machine may be a bit overkill, but I hate nested if
s and elif
s.
This situation is analogous to the one found in the book Expert C Programming, Chapter 2, by Peter van der Linden:
char *available_resources[] = {
"color monitor",
"big disk",
"Cray" /* whoa! no comma! */
"on-line drawing routines",
"mouse",
"keyboard",
"power cables", /* and what's this extra comma? */
};
It would be a mistake to assume the resources include a Cray supercomputer.
vim-checkimpcont, a Vim plugin.
The code is in public domain.