org.sonar.l10n.py.rules.python.S5799.html Maven / Gradle / Ivy
The newest version!
This rule raises an issue when strings or bytes are concatenated implicitly.
Why is this an issue?
Python concatenates adjacent string or byte literals at compile time. It means that "a" "b"
is equivalent to "ab"
. This
is sometimes used to split a long string on multiple lines. However an implicit string concatenation can also be very confusing. In the following
contexts it might indicate that a comma was forgotten:
- when the two strings are on the same line. This could be interpreted as an incorrectly formatted tuple (parentheses are not mandatory to create
a tuple, only the comma is).
- when the strings are in a list, a set or a tuple.
Code examples
Noncompliant code example
def func():
return "item1" "item2" # Noncompliant: a comma is missing to return a tuple.
["1" # Noncompliant: a comma is missing.
"2",
"a very" # Noncompliant: a "+" is missing.
"long string"]
Compliant solution
def func():
return "item1", "item2"
["1",
"2",
"a very" +
"long string"]
Exceptions
This rule will not raise any issues when there is a visible reason for the string concatenation:
- when the quotes used for both strings are different. This can be used to avoid escaping quotes.
- when the strings or bytes have different prefixes, i.e. "f" for f-strings, "r" for raw, "u" for unicode and no prefix for normal strings.
- when strings are visibly split to avoid long lines of code, i.e. when the first string ends with a space, punctuation or
\n
.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy