summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2018-06-18 08:31:47 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2018-06-18 08:31:47 +0200
commit2c28f5252cacf28ec2e3174088be203254eccd6f (patch)
treeb0ca3179ec5ea06f0bbbacef9efd979ead1ec1ce
parentbdbf5561bf24523c234ec966aaa629c7e6f703d7 (diff)
downloadpylint-git-2c28f5252cacf28ec2e3174088be203254eccd6f.tar.gz
`logging-not-lazy` is emitted whenever pylint infers that a string is built with addition
Close #2193
-rw-r--r--ChangeLog5
-rw-r--r--pylint/checkers/logging.py11
-rw-r--r--pylint/test/functional/logging_not_lazy.py5
-rw-r--r--pylint/test/functional/logging_not_lazy.txt4
4 files changed, 19 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 2a500b788..172902765 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,11 @@ What's New in Pylint 2.0?
=========================
Release date: |TBA|
+
+ * `logging-not-lazy` is emitted whenever pylint infers that a string is built with addition
+
+ Close #2193
+
* Add a check `chained-comparison` which is emitted if a boolean operation can be simplified
by chaining some of its operations.
e.g "a < b and b < c", can be simplified as "a < b < c".
diff --git a/pylint/checkers/logging.py b/pylint/checkers/logging.py
index d27cb0eb5..3129a5e63 100644
--- a/pylint/checkers/logging.py
+++ b/pylint/checkers/logging.py
@@ -193,9 +193,14 @@ class LoggingChecker(checkers.BaseChecker):
if isinstance(node.args[format_pos], astroid.BinOp):
binop = node.args[format_pos]
- if (binop.op == '%' or binop.op == '+' and
- len([_operand for _operand in (binop.left, binop.right)
- if self._is_operand_literal_str(_operand)]) == 1):
+ emit = binop.op == '%'
+ if binop.op == '+':
+ total_number_of_strings = sum(
+ 1 for operand in (binop.left, binop.right)
+ if self._is_operand_literal_str(utils.safe_infer(operand))
+ )
+ emit = total_number_of_strings > 0
+ if emit:
self.add_message('logging-not-lazy', node=node)
elif isinstance(node.args[format_pos], astroid.Call):
self._check_call_func(node.args[format_pos])
diff --git a/pylint/test/functional/logging_not_lazy.py b/pylint/test/functional/logging_not_lazy.py
index 98eaee08e..c0634327f 100644
--- a/pylint/test/functional/logging_not_lazy.py
+++ b/pylint/test/functional/logging_not_lazy.py
@@ -5,18 +5,19 @@ import logging as renamed_logging
import os as logging
var = "123"
+var_name = 'Var:'
# Statements that should be flagged:
renamed_logging.warn('%s, %s' % (4, 5)) # [logging-not-lazy]
renamed_logging.exception('%s' % 'Exceptional!') # [logging-not-lazy]
renamed_logging.log(renamed_logging.INFO, 'msg: %s' % 'Run!') # [logging-not-lazy]
renamed_logging.log(renamed_logging.INFO, "Var: " + var) # [logging-not-lazy]
+renamed_logging.warn('%s' + ' the rest of a single string') # [logging-not-lazy]
+renamed_logging.log(renamed_logging.INFO, var_name + var) # [logging-not-lazy]
var_name = 'Var:'
# Statements that should not be flagged:
renamed_logging.warn('%s, %s', 4, 5)
renamed_logging.log(renamed_logging.INFO, 'msg: %s', 'Run!')
-renamed_logging.warn('%s' + ' the rest of a single string')
-renamed_logging.log(renamed_logging.INFO, var_name + var)
logging.warn('%s, %s' % (4, 5))
logging.log(logging.INFO, 'msg: %s' % 'Run!')
logging.log("Var: " + var)
diff --git a/pylint/test/functional/logging_not_lazy.txt b/pylint/test/functional/logging_not_lazy.txt
index 5a91369d9..31e7cc448 100644
--- a/pylint/test/functional/logging_not_lazy.txt
+++ b/pylint/test/functional/logging_not_lazy.txt
@@ -1,4 +1,6 @@
-logging-not-lazy:9::Specify string format arguments as logging function parameters
logging-not-lazy:10::Specify string format arguments as logging function parameters
logging-not-lazy:11::Specify string format arguments as logging function parameters
logging-not-lazy:12::Specify string format arguments as logging function parameters
+logging-not-lazy:13::Specify string format arguments as logging function parameters
+logging-not-lazy:14::Specify string format arguments as logging function parameters
+logging-not-lazy:15::Specify string format arguments as logging function parameters