summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorguillaume2 <guillaume.peillex@gmail.col>2017-07-20 20:56:55 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2017-07-23 14:10:03 +0200
commitd7260de1f4be5b9149c814eaae0f65a349a9cd88 (patch)
tree3a12bd879b3fd5bc1beb5cccc39d3bf51844fb67
parent35983f6cf30ad69522de4f6e30ce14c4c1cd7e6c (diff)
downloadpylint-git-d7260de1f4be5b9149c814eaae0f65a349a9cd88.tar.gz
logging-not-lazy is emitted when the addition sign is used as well
Close #1479
-rw-r--r--ChangeLog3
-rw-r--r--doc/whatsnew/1.8.rst17
-rw-r--r--pylint/checkers/logging.py15
-rw-r--r--pylint/test/functional/logging_not_lazy.py7
-rw-r--r--pylint/test/functional/logging_not_lazy.txt3
5 files changed, 41 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 5ffd135d1..1072d4239 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -52,6 +52,9 @@ What's New in Pylint 1.8?
* Added a new warning, ``raising-format-tuple``, to detect multi-argument
exception construction instead of message string formatting.
+ * Added a new check for method of logging module that concatenate string via + operator
+ Close #1479
+
What's New in Pylint 1.7.1?
=========================
diff --git a/doc/whatsnew/1.8.rst b/doc/whatsnew/1.8.rst
index f7501ffb2..ba54af9b6 100644
--- a/doc/whatsnew/1.8.rst
+++ b/doc/whatsnew/1.8.rst
@@ -165,6 +165,23 @@ New checkers
.. _PEP479: https://www.python.org/dev/peps/pep-0479
+* A new Python checker was added to warn about using a ``+`` operator inside call of logging methods
+ when one of the operands is a litteral string:
+
+ .. code-block:: python
+
+ import logging
+ var = "123"
+ logging.log(logging.INFO, "Var: " + var)
+
+ Instead use formatted string and positional arguments :
+
+ .. code-block:: python
+
+ import logging
+ var = "123"
+ logging.log(logging.INFO, "Var: %s", var)
+
Other Changes
=============
diff --git a/pylint/checkers/logging.py b/pylint/checkers/logging.py
index 96edb1d24..f0e7e6440 100644
--- a/pylint/checkers/logging.py
+++ b/pylint/checkers/logging.py
@@ -174,13 +174,24 @@ class LoggingChecker(checkers.BaseChecker):
else:
return
- if isinstance(node.args[format_pos], astroid.BinOp) and node.args[format_pos].op == '%':
- self.add_message('logging-not-lazy', node=node)
+ 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):
+ self.add_message('logging-not-lazy', node=node)
elif isinstance(node.args[format_pos], astroid.Call):
self._check_call_func(node.args[format_pos])
elif isinstance(node.args[format_pos], astroid.Const):
self._check_format_string(node, format_pos)
+ @staticmethod
+ def _is_operand_literal_str(operand):
+ """
+ Return True if the operand in argument is a literal string
+ """
+ return isinstance(operand, astroid.Const) and operand.name == 'str'
+
def _check_call_func(self, node):
"""Checks that function call is not format_string.format().
diff --git a/pylint/test/functional/logging_not_lazy.py b/pylint/test/functional/logging_not_lazy.py
index b843431e4..98eaee08e 100644
--- a/pylint/test/functional/logging_not_lazy.py
+++ b/pylint/test/functional/logging_not_lazy.py
@@ -1,17 +1,22 @@
-# pylint: disable=missing-docstring,no-member,deprecated-method
+# pylint: disable=missing-docstring,no-member,deprecated-method,invalid-name
# Muck up the names in an effort to confuse...
import logging as renamed_logging
import os as logging
+var = "123"
# 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]
+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 60c6c5907..5a91369d9 100644
--- a/pylint/test/functional/logging_not_lazy.txt
+++ b/pylint/test/functional/logging_not_lazy.txt
@@ -1,3 +1,4 @@
-logging-not-lazy:8::Specify string format arguments as logging function parameters
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