Skip to content

Check string literal concatenation by implicit line continuation in Python souce.

Notifications You must be signed in to change notification settings

congma/checkimpcont

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Check string literal concatenation by implicit line continuation in Python souce.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published