2023-08-04 16:15:13 +02:00
This rule raises an issue when strings or bytes are concatenated implicitly.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-08-04 16:15:13 +02:00
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.
2021-04-28 16:49:39 +02:00
2023-08-04 16:15:13 +02:00
=== Code examples
2021-04-28 18:08:03 +02:00
2023-08-04 16:15:13 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-08-04 16:15:13 +02:00
[source,python,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
def func():
2023-08-04 16:15:13 +02:00
return "item1" "item2" # Noncompliant: a comma is missing to return a tuple.
2021-04-28 16:49:39 +02:00
2023-08-04 16:15:13 +02:00
["1" # Noncompliant: a comma is missing.
"2",
"a very" # Noncompliant: a "+" is missing.
"long string"]
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-08-04 16:15:13 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-08-04 16:15:13 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
def func():
return "item1", "item2"
["1",
"2",
2023-08-04 16:15:13 +02:00
"a very" +
"long string"]
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-08-04 16:15:13 +02:00
This rule will not raise any issues when there is a visible reason for the string concatenation:
2021-04-28 16:49:39 +02:00
2023-08-04 16:15:13 +02:00
* when the quotes used for both strings are different. This can be used to avoid escaping quotes.
2021-04-28 16:49:39 +02:00
* 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.
2023-08-04 16:15:13 +02:00
* when strings are visibly split to avoid long lines of code, i.e. when the first string ends with a space, punctuation or ``++\n++``.
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
When the strings are on multiple lines:
* "Add a "+" operator to make the string concatenation explicit; or did you forget a comma?"
When the strings are on the same line:
* "Merge these implicitly concatenated strings; or did you forget a comma?"
=== Highlighting
Primary: The end quote of the first concatenated string/bytes literal
Secondary:
* location: the starting quote of the next string/bytes/literal
* no message
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]