Using what @wjandrea found, here is a possible solution. It is not pretty, but it can correctly detect that this code has implicit string concatenation. I had it just print out the warning instead of crashing, but if you want it could be modified to throw an error.
You could also just run pylint
on your file, but I assumed you were looking for a "programmatic" way to detect when this, and only this, occurs.
import pylint.lint
import sys
def check_for_implicit_str_concat():
pylint.lint.reporters.json_reporter.JSONReporter.display_messages = lambda self, layout: None
options = [
sys.argv[0],
'--output-format=pylint.reporters.json_reporter.JSONReporter',
'--check-str-concat-over-line-jumps=y'
]
results = pylint.lint.Run(options, do_exit=False)
for message in results.linter.reporter.messages:
if message['message-id'] == 'W1403':
print(message)
if __name__ == "__main__":
check_for_implicit_str_concat()
COLOURS = [
"White",
"Black",
"Red"
"Green",
"Blue"
]
Output:
{
"type": "warning",
"module": "test",
"obj": "",
"line": 22,
"column": 0,
"path": "test.py",
"symbol": "implicit-str-concat-in-sequence",
"message": "Implicit string concatenation found in list",
"message-id": "W1403"
}
check-str-concat-over-line-jumps
that will find it even if the strings aren't on the same line. – NoranorahCOLORS = "White Black Red Green Blue".split()
. For any list whose elements you were explicitly enumerating, the overhead should be minimal. (That is, I'm assuming you weren't defining a list of hundreds of elements or more.) – Rhizogenic