Skip to content

Latest commit

 

History

History
80 lines (59 loc) · 2.13 KB

README.md

File metadata and controls

80 lines (59 loc) · 2.13 KB

Build Status

checkimpcont.py

Check concatenated string literals in Python source code.

Usage

checkimpcont.py [file]

Example

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 "
                                        ~~~^

Installation

python setup.py install
# -- or --
pip install .

Rationale

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:

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 ifs and elifs.

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.

See Also

vim-checkimpcont, a Vim plugin.

License

The code is in public domain.