This rule raises an issue when a Template object is concatenated directly with a str object using the + operator.
Direct concatenation between Template and str objects is prohibited in Python because it creates ambiguity about how the string should be interpreted.
When you concatenate a Template with a str, it’s unclear whether the str should be:
This ambiguity can lead to unexpected behavior and makes the code’s intent unclear. Python’s PEP 750 specification explicitly prohibits this operation to force developers to be explicit about their intentions.
The Template type is designed to clearly separate static string parts from dynamic interpolations, and allowing direct concatenation with str would undermine this design principle.
Direct concatenation of Template and str objects will result in a runtime error, causing the application to fail. Additionally, even if it were allowed, the ambiguous nature of such operations could lead to security vulnerabilities or incorrect string processing, especially in contexts like HTML templating or SQL query construction where the distinction between static and dynamic content is critical.
If the string should be treated as static text, wrap it in a Template constructor before concatenation.
name = "World" template = t"Hello " + name # Noncompliant
name = "World" template = t"Hello " + Template(name)