diff options
Diffstat (limited to 'checkers/logging.py')
-rw-r--r-- | checkers/logging.py | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/checkers/logging.py b/checkers/logging.py index 277b8c0..897c1c7 100644 --- a/checkers/logging.py +++ b/checkers/logging.py @@ -35,14 +35,14 @@ MSGS = { 'interpolation in those cases in which no message will be ' 'logged. For more, see ' 'http://www.python.org/dev/peps/pep-0282/.'), - 'W1202': ('Use % formatting in logging functions but pass the % ' - 'parameters as arguments', - 'logging-format-interpolation', - 'Used when a logging statement has a call form of ' - '"logging.<logging method>(format_string.format(format_args...))"' - '. Such calls should use % formatting instead, but leave ' - 'interpolation to the logging function by passing the parameters ' - 'as arguments.'), + 'W1202': ('Use % formatting in logging functions but pass the % ' + 'parameters as arguments', + 'logging-format-interpolation', + 'Used when a logging statement has a call form of ' + '"logging.<logging method>(format_string.format(format_args...))"' + '. Such calls should use % formatting instead, but leave ' + 'interpolation to the logging function by passing the parameters ' + 'as arguments.'), 'E1200': ('Unsupported logging format character %r (%#02x) at index %d', 'logging-unsupported-format', 'Used when an unsupported format character is used in a logging\ @@ -64,6 +64,27 @@ CHECKED_CONVENIENCE_FUNCTIONS = set([ 'critical', 'debug', 'error', 'exception', 'fatal', 'info', 'warn', 'warning']) +def is_method_call(callfunc_node, types=(), methods=()): + """Determines if a CallFunc node represents a method call. + + Args: + callfunc_node: The CallFunc AST node to check. + types: Optional sequence of caller type names to restrict check. + methods: Optional sequence of method names to restrict check. + + Returns: + True, if the node represents a method call for the given type and + method names, False otherwise. + """ + if not isinstance(callfunc_node, astroid.CallFunc): + return False + func = utils.safe_infer(callfunc_node.func) + return (isinstance(func, astroid.BoundMethod) + and isinstance(func.bound, astroid.Instance) + and (func.bound.name in types if types else True) + and (func.name in methods if methods else True)) + + class LoggingChecker(checkers.BaseChecker): """Checks use of the logging module.""" @@ -81,7 +102,7 @@ class LoggingChecker(checkers.BaseChecker): ), ) - def visit_module(self, unused_node): + def visit_module(self, node): # pylint: disable=unused-argument """Clears any state left in this checker from last module checked.""" # The code being checked can just as easily "import logging as foo", # so it is necessary to process the imports and store in this field @@ -142,26 +163,6 @@ class LoggingChecker(checkers.BaseChecker): return self._check_log_method(node, name) - def is_method_call(self, callfunc_node, types=(), methods=()): - """Determines if a CallFunc node represents a method call. - - Args: - callfunc_node: The CallFunc AST node to check. - types: Optional sequence of caller type names to restrict check. - methods: Optional sequence of method names to restrict check. - - Returns: - True, if the node represents a method call for the given type and - method names, False otherwise. - """ - if not isinstance(callfunc_node, astroid.CallFunc): - return False - func = utils.safe_infer(callfunc_node.func) - return (isinstance(func, astroid.BoundMethod) - and isinstance(func.bound, astroid.Instance) - and (func.bound.name in types if types else True) - and (func.name in methods if methods else True)) - def _check_log_method(self, node, name): """Checks calls to logging.log(level, format, *format_args).""" if name == 'log': @@ -192,7 +193,7 @@ class LoggingChecker(checkers.BaseChecker): Args: callfunc_node: CallFunc AST node to be checked. """ - if self.is_method_call(callfunc_node, ('str', 'unicode'), ('format',)): + if is_method_call(callfunc_node, ('str', 'unicode'), ('format',)): self.add_message('logging-format-interpolation', node=callfunc_node) def _check_format_string(self, node, format_arg): |