Skip to content

Commit 011c6ac

Browse files
yileiDanielNoord
authored andcommitted
Do not emit logging-not-lazy for explicitly concatenated strings. (#8546)
(cherry picked from commit eeddd66)
1 parent 84d4959 commit 011c6ac

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`logging-not-lazy` is not longer emitted for explicitly concatenated string arguments.
2+
3+
Closes #8410

pylint/checkers/logging.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def _check_log_method(self, node: nodes.Call, name: str) -> None:
246246
if isinstance(format_arg, nodes.BinOp):
247247
binop = format_arg
248248
emit = binop.op == "%"
249-
if binop.op == "+":
249+
if binop.op == "+" and not self._is_node_explicit_str_concatenation(binop):
250250
total_number_of_strings = sum(
251251
1
252252
for operand in (binop.left, binop.right)
@@ -294,6 +294,19 @@ def _is_operand_literal_str(operand: InferenceResult | None) -> bool:
294294
"""Return True if the operand in argument is a literal string."""
295295
return isinstance(operand, nodes.Const) and operand.name == "str"
296296

297+
@staticmethod
298+
def _is_node_explicit_str_concatenation(node: nodes.NodeNG) -> bool:
299+
"""Return True if the node represents an explicitly concatenated string."""
300+
if not isinstance(node, nodes.BinOp):
301+
return False
302+
return (
303+
LoggingChecker._is_operand_literal_str(node.left)
304+
or LoggingChecker._is_node_explicit_str_concatenation(node.left)
305+
) and (
306+
LoggingChecker._is_operand_literal_str(node.right)
307+
or LoggingChecker._is_node_explicit_str_concatenation(node.right)
308+
)
309+
297310
def _check_call_func(self, node: nodes.Call) -> None:
298311
"""Checks that function call is not format_string.format()."""
299312
func = utils.safe_infer(node.func)

tests/functional/l/logging/logging_not_lazy.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
var_name = "Var:"
1010
# Statements that should be flagged:
1111
renamed_logging.warn("%s, %s" % (4, 5)) # [logging-not-lazy]
12+
renamed_logging.warn("Var: " + var) # [logging-not-lazy]
1213
renamed_logging.exception("%s" % "Exceptional!") # [logging-not-lazy]
1314
renamed_logging.log(renamed_logging.INFO, "msg: %s" % "Run!") # [logging-not-lazy]
1415
renamed_logging.log(renamed_logging.INFO, "Var: " + var) # [logging-not-lazy]
15-
renamed_logging.warn("%s" + " the rest of a single string") # [logging-not-lazy]
1616
renamed_logging.log(renamed_logging.INFO, var_name + var) # [logging-not-lazy]
1717

1818
# Statements that should not be flagged:
@@ -21,6 +21,11 @@
2121
logging.warn("%s, %s" % (4, 5))
2222
logging.log(logging.INFO, "msg: %s" % "Run!")
2323
logging.log("Var: " + var)
24+
# Explicit string concatenations are fine:
25+
renamed_logging.warn("%s" + " the rest of a single string")
26+
renamed_logging.warn("Msg: " + "%s", "first piece " + "second piece")
27+
renamed_logging.warn("first" + "second" + "third %s", "parameter")
28+
renamed_logging.warn(("first" + "second" + "third %s"))
2429

2530
# Regression crash test for incorrect format call
2631
renamed_logging.error(
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
logging-not-lazy:11:0:11:39::Use lazy % formatting in logging functions:UNDEFINED
2-
logging-not-lazy:12:0:12:48::Use lazy % formatting in logging functions:UNDEFINED
3-
logging-not-lazy:13:0:13:61::Use lazy % formatting in logging functions:UNDEFINED
4-
logging-not-lazy:14:0:14:56::Use lazy % formatting in logging functions:UNDEFINED
5-
logging-not-lazy:15:0:15:59::Use lazy % formatting in logging functions:UNDEFINED
2+
logging-not-lazy:12:0:12:35::Use lazy % formatting in logging functions:UNDEFINED
3+
logging-not-lazy:13:0:13:48::Use lazy % formatting in logging functions:UNDEFINED
4+
logging-not-lazy:14:0:14:61::Use lazy % formatting in logging functions:UNDEFINED
5+
logging-not-lazy:15:0:15:56::Use lazy % formatting in logging functions:UNDEFINED
66
logging-not-lazy:16:0:16:57::Use lazy % formatting in logging functions:UNDEFINED
7-
bad-format-string:27:4:27:27::Invalid format string:UNDEFINED
8-
logging-format-interpolation:27:4:27:27::Use lazy % formatting in logging functions:UNDEFINED
7+
bad-format-string:32:4:32:27::Invalid format string:UNDEFINED
8+
logging-format-interpolation:32:4:32:27::Use lazy % formatting in logging functions:UNDEFINED

0 commit comments

Comments
 (0)