Index: third_party/pylint/utils.py
|
diff --git a/third_party/pylint/utils.py b/third_party/pylint/utils.py
|
index b476dc1ba7fb0a436486d0f5d14bcbcc22033632..fcc790403dc99032413a272044c2f6c0379ed10d 100644
|
--- a/third_party/pylint/utils.py
|
+++ b/third_party/pylint/utils.py
|
@@ -16,14 +16,19 @@
|
"""some various utilities and helper classes, most of them used in the
|
main pylint class
|
"""
|
+from __future__ import print_function
|
|
+import collections
|
+import os
|
import re
|
import sys
|
import tokenize
|
-import os
|
-from warnings import warn
|
+import warnings
|
from os.path import dirname, basename, splitext, exists, isdir, join, normpath
|
|
+import six
|
+from six.moves import zip # pylint: disable=redefined-builtin
|
+
|
from logilab.common.interface import implements
|
from logilab.common.textutils import normalize_text
|
from logilab.common.configuration import rest_format_section
|
@@ -33,7 +38,7 @@ from astroid import nodes, Module
|
from astroid.modutils import modpath_from_file, get_module_files, \
|
file_from_modpath, load_module_from_file
|
|
-from pylint.interfaces import IRawChecker, ITokenChecker
|
+from pylint.interfaces import IRawChecker, ITokenChecker, UNDEFINED
|
|
|
class UnknownMessage(Exception):
|
@@ -51,7 +56,7 @@ MSG_TYPES = {
|
'E' : 'error',
|
'F' : 'fatal'
|
}
|
-MSG_TYPES_LONG = dict([(v, k) for k, v in MSG_TYPES.iteritems()])
|
+MSG_TYPES_LONG = {v: k for k, v in six.iteritems(MSG_TYPES)}
|
|
MSG_TYPES_STATUS = {
|
'I' : 0,
|
@@ -65,6 +70,7 @@ MSG_TYPES_STATUS = {
|
_MSG_ORDER = 'EWRCIF'
|
MSG_STATE_SCOPE_CONFIG = 0
|
MSG_STATE_SCOPE_MODULE = 1
|
+MSG_STATE_CONFIDENCE = 2
|
|
OPTION_RGX = re.compile(r'\s*#.*\bpylint:(.*)')
|
|
@@ -75,6 +81,29 @@ class WarningScope(object):
|
LINE = 'line-based-msg'
|
NODE = 'node-based-msg'
|
|
+_MsgBase = collections.namedtuple(
|
+ '_MsgBase',
|
+ ['msg_id', 'symbol', 'msg', 'C', 'category', 'confidence',
|
+ 'abspath', 'path', 'module', 'obj', 'line', 'column'])
|
+
|
+
|
+class Message(_MsgBase):
|
+ """This class represent a message to be issued by the reporters"""
|
+ def __new__(cls, msg_id, symbol, location, msg, confidence):
|
+ return _MsgBase.__new__(
|
+ cls, msg_id, symbol, msg, msg_id[0], MSG_TYPES[msg_id[0]],
|
+ confidence, *location)
|
+
|
+ def format(self, template):
|
+ """Format the message according to the given template.
|
+
|
+ The template format is the one of the format method :
|
+ cf. http://docs.python.org/2/library/string.html#formatstrings
|
+ """
|
+ # For some reason, _asdict on derived namedtuples does not work with
|
+ # Python 3.4. Needs some investigation.
|
+ return template.format(**dict(zip(self._fields, self)))
|
+
|
|
def get_module_and_frameid(node):
|
"""return the module name and the frame id in the module"""
|
@@ -92,11 +121,11 @@ def get_module_and_frameid(node):
|
obj.reverse()
|
return module, '.'.join(obj)
|
|
-def category_id(id):
|
- id = id.upper()
|
- if id in MSG_TYPES:
|
- return id
|
- return MSG_TYPES_LONG.get(id)
|
+def category_id(cid):
|
+ cid = cid.upper()
|
+ if cid in MSG_TYPES:
|
+ return cid
|
+ return MSG_TYPES_LONG.get(cid)
|
|
|
def tokenize_module(module):
|
@@ -124,8 +153,8 @@ def build_message_def(checker, msgid, msg_tuple):
|
# messages should have a symbol, but for backward compatibility
|
# they may not.
|
(msg, descr) = msg_tuple
|
- warn("[pylint 0.26] description of message %s doesn't include "
|
- "a symbolic name" % msgid, DeprecationWarning)
|
+ warnings.warn("[pylint 0.26] description of message %s doesn't include "
|
+ "a symbolic name" % msgid, DeprecationWarning)
|
symbol = None
|
options.setdefault('scope', default_scope)
|
return MessageDefinition(checker, msgid, msg, descr, symbol, **options)
|
@@ -238,7 +267,7 @@ class MessagesHandlerMixIn(object):
|
msgs = self._msgs_state
|
msgs[msg.msgid] = False
|
# sync configuration object
|
- self.config.disable_msg = [mid for mid, val in msgs.iteritems()
|
+ self.config.disable_msg = [mid for mid, val in six.iteritems(msgs)
|
if not val]
|
|
def enable(self, msgid, scope='package', line=None, ignore_unknown=False):
|
@@ -276,14 +305,27 @@ class MessagesHandlerMixIn(object):
|
msgs = self._msgs_state
|
msgs[msg.msgid] = True
|
# sync configuration object
|
- self.config.enable = [mid for mid, val in msgs.iteritems() if val]
|
+ self.config.enable = [mid for mid, val in six.iteritems(msgs) if val]
|
+
|
+ def get_message_state_scope(self, msgid, line=None, confidence=UNDEFINED):
|
+ """Returns the scope at which a message was enabled/disabled."""
|
+ if self.config.confidence and confidence.name not in self.config.confidence:
|
+ return MSG_STATE_CONFIDENCE
|
+ try:
|
+ if line in self.file_state._module_msgs_state[msgid]:
|
+ return MSG_STATE_SCOPE_MODULE
|
+ except (KeyError, TypeError):
|
+ return MSG_STATE_SCOPE_CONFIG
|
|
- def is_message_enabled(self, msg_descr, line=None):
|
+ def is_message_enabled(self, msg_descr, line=None, confidence=None):
|
"""return true if the message associated to the given message id is
|
enabled
|
|
msgid may be either a numeric or symbolic message id.
|
"""
|
+ if self.config.confidence and confidence:
|
+ if confidence.name not in self.config.confidence:
|
+ return False
|
try:
|
msgid = self.msgs_store.check_message_id(msg_descr).msgid
|
except UnknownMessage:
|
@@ -298,7 +340,7 @@ class MessagesHandlerMixIn(object):
|
except KeyError:
|
return self._msgs_state.get(msgid, True)
|
|
- def add_message(self, msg_descr, line=None, node=None, args=None):
|
+ def add_message(self, msg_descr, line=None, node=None, args=None, confidence=UNDEFINED):
|
"""Adds a message given by ID or name.
|
|
If provided, the message string is expanded using args
|
@@ -328,8 +370,10 @@ class MessagesHandlerMixIn(object):
|
else:
|
col_offset = None
|
# should this message be displayed
|
- if not self.is_message_enabled(msgid, line):
|
- self.file_state.handle_ignored_message(msgid, line, node, args)
|
+ if not self.is_message_enabled(msgid, line, confidence):
|
+ self.file_state.handle_ignored_message(
|
+ self.get_message_state_scope(msgid, line, confidence),
|
+ msgid, line, node, args, confidence)
|
return
|
# update stats
|
msg_cat = MSG_TYPES[msgid[0]]
|
@@ -347,31 +391,37 @@ class MessagesHandlerMixIn(object):
|
# get module and object
|
if node is None:
|
module, obj = self.current_name, ''
|
- path = self.current_file
|
+ abspath = self.current_file
|
else:
|
module, obj = get_module_and_frameid(node)
|
- path = node.root().file
|
+ abspath = node.root().file
|
+ path = abspath.replace(self.reporter.path_strip_prefix, '')
|
# add the message
|
- self.reporter.add_message(msgid, (path, module, obj, line or 1, col_offset or 0), msg)
|
+ self.reporter.handle_message(
|
+ Message(msgid, symbol,
|
+ (abspath, path, module, obj, line or 1, col_offset or 0), msg, confidence))
|
|
def print_full_documentation(self):
|
"""output a full documentation in ReST format"""
|
+ print("Pylint global options and switches")
|
+ print("----------------------------------")
|
+ print("")
|
+ print("Pylint provides global options and switches.")
|
+ print("")
|
+
|
by_checker = {}
|
for checker in self.get_checkers():
|
if checker.name == 'master':
|
- prefix = 'Main '
|
- print "Options"
|
- print '-------\n'
|
if checker.options:
|
for section, options in checker.options_by_section():
|
if section is None:
|
title = 'General options'
|
else:
|
title = '%s options' % section.capitalize()
|
- print title
|
- print '~' * len(title)
|
+ print(title)
|
+ print('~' * len(title))
|
rest_format_section(sys.stdout, None, options)
|
- print
|
+ print("")
|
else:
|
try:
|
by_checker[checker.name][0] += checker.options_and_values()
|
@@ -381,35 +431,49 @@ class MessagesHandlerMixIn(object):
|
by_checker[checker.name] = [list(checker.options_and_values()),
|
dict(checker.msgs),
|
list(checker.reports)]
|
- for checker, (options, msgs, reports) in by_checker.iteritems():
|
- prefix = ''
|
- title = '%s checker' % checker
|
- print title
|
- print '-' * len(title)
|
- print
|
+
|
+ print("Pylint checkers' options and switches")
|
+ print("-------------------------------------")
|
+ print("")
|
+ print("Pylint checkers can provide three set of features:")
|
+ print("")
|
+ print("* options that control their execution,")
|
+ print("* messages that they can raise,")
|
+ print("* reports that they can generate.")
|
+ print("")
|
+ print("Below is a list of all checkers and their features.")
|
+ print("")
|
+
|
+ for checker, (options, msgs, reports) in six.iteritems(by_checker):
|
+ title = '%s checker' % (checker.replace("_", " ").title())
|
+ print(title)
|
+ print('~' * len(title))
|
+ print("")
|
+ print("Verbatim name of the checker is ``%s``." % checker)
|
+ print("")
|
if options:
|
title = 'Options'
|
- print title
|
- print '~' * len(title)
|
+ print(title)
|
+ print('^' * len(title))
|
rest_format_section(sys.stdout, None, options)
|
- print
|
+ print("")
|
if msgs:
|
- title = ('%smessages' % prefix).capitalize()
|
- print title
|
- print '~' * len(title)
|
- for msgid, msg in sorted(msgs.iteritems(),
|
- key=lambda (k, v): (_MSG_ORDER.index(k[0]), k)):
|
+ title = 'Messages'
|
+ print(title)
|
+ print('~' * len(title))
|
+ for msgid, msg in sorted(six.iteritems(msgs),
|
+ key=lambda kv: (_MSG_ORDER.index(kv[0][0]), kv[1])):
|
msg = build_message_def(checker, msgid, msg)
|
- print msg.format_help(checkerref=False)
|
- print
|
+ print(msg.format_help(checkerref=False))
|
+ print("")
|
if reports:
|
- title = ('%sreports' % prefix).capitalize()
|
- print title
|
- print '~' * len(title)
|
+ title = 'Reports'
|
+ print(title)
|
+ print('~' * len(title))
|
for report in reports:
|
- print ':%s: %s' % report[:2]
|
- print
|
- print
|
+ print(':%s: %s' % report[:2])
|
+ print("")
|
+ print("")
|
|
|
class FileState(object):
|
@@ -419,12 +483,12 @@ class FileState(object):
|
self.base_name = modname
|
self._module_msgs_state = {}
|
self._raw_module_msgs_state = {}
|
- self._ignored_msgs = {}
|
+ self._ignored_msgs = collections.defaultdict(set)
|
self._suppression_mapping = {}
|
|
def collect_block_lines(self, msgs_store, module_node):
|
"""Walk the AST to collect block level options line numbers."""
|
- for msg, lines in self._module_msgs_state.iteritems():
|
+ for msg, lines in six.iteritems(self._module_msgs_state):
|
self._raw_module_msgs_state[msg] = lines.copy()
|
orig_state = self._module_msgs_state.copy()
|
self._module_msgs_state = {}
|
@@ -458,8 +522,8 @@ class FileState(object):
|
firstchildlineno = node.body[0].fromlineno
|
else:
|
firstchildlineno = last
|
- for msgid, lines in msg_state.iteritems():
|
- for lineno, state in lines.items():
|
+ for msgid, lines in six.iteritems(msg_state):
|
+ for lineno, state in list(lines.items()):
|
original_lineno = lineno
|
if first <= lineno <= last:
|
# Set state for all lines for this block, if the
|
@@ -471,7 +535,7 @@ class FileState(object):
|
else:
|
first_ = lineno
|
last_ = last
|
- for line in xrange(first_, last_+1):
|
+ for line in range(first_, last_+1):
|
# do not override existing entries
|
if not line in self._module_msgs_state.get(msgid, ()):
|
if line in lines: # state change in the same block
|
@@ -493,37 +557,29 @@ class FileState(object):
|
except KeyError:
|
self._module_msgs_state[msg.msgid] = {line: status}
|
|
- def handle_ignored_message(self, msgid, line, node, args):
|
+ def handle_ignored_message(self, state_scope, msgid, line,
|
+ node, args, confidence): # pylint: disable=unused-argument
|
"""Report an ignored message.
|
|
state_scope is either MSG_STATE_SCOPE_MODULE or MSG_STATE_SCOPE_CONFIG,
|
depending on whether the message was disabled locally in the module,
|
or globally. The other arguments are the same as for add_message.
|
"""
|
- state_scope = self._message_state_scope(msgid, line)
|
if state_scope == MSG_STATE_SCOPE_MODULE:
|
try:
|
orig_line = self._suppression_mapping[(msgid, line)]
|
- self._ignored_msgs.setdefault((msgid, orig_line), set()).add(line)
|
+ self._ignored_msgs[(msgid, orig_line)].add(line)
|
except KeyError:
|
pass
|
|
- def _message_state_scope(self, msgid, line=None):
|
- """Returns the scope at which a message was enabled/disabled."""
|
- try:
|
- if line in self._module_msgs_state[msgid]:
|
- return MSG_STATE_SCOPE_MODULE
|
- except KeyError:
|
- return MSG_STATE_SCOPE_CONFIG
|
-
|
def iter_spurious_suppression_messages(self, msgs_store):
|
- for warning, lines in self._raw_module_msgs_state.iteritems():
|
- for line, enable in lines.iteritems():
|
+ for warning, lines in six.iteritems(self._raw_module_msgs_state):
|
+ for line, enable in six.iteritems(lines):
|
if not enable and (warning, line) not in self._ignored_msgs:
|
yield 'useless-suppression', line, \
|
(msgs_store.get_msg_display_string(warning),)
|
# don't use iteritems here, _ignored_msgs may be modified by add_message
|
- for (warning, from_), lines in self._ignored_msgs.items():
|
+ for (warning, from_), lines in list(self._ignored_msgs.items()):
|
for line in lines:
|
yield 'suppressed-message', line, \
|
(msgs_store.get_msg_display_string(warning), from_)
|
@@ -544,12 +600,12 @@ class MessagesStore(object):
|
# message definitions. May contain several names for each definition
|
# object.
|
self._alternative_names = {}
|
- self._msgs_by_category = {}
|
+ self._msgs_by_category = collections.defaultdict(list)
|
|
@property
|
def messages(self):
|
"""The list of all active messages."""
|
- return self._messages.itervalues()
|
+ return six.itervalues(self._messages)
|
|
def add_renamed_message(self, old_id, old_symbol, new_symbol):
|
"""Register the old ID and symbol for a warning that was renamed.
|
@@ -571,7 +627,7 @@ class MessagesStore(object):
|
are the checker id and the two last the message id in this checker
|
"""
|
chkid = None
|
- for msgid, msg_tuple in checker.msgs.iteritems():
|
+ for msgid, msg_tuple in six.iteritems(checker.msgs):
|
msg = build_message_def(checker, msgid, msg_tuple)
|
assert msg.symbol not in self._messages, \
|
'Message symbol %r is already defined' % msg.symbol
|
@@ -586,7 +642,7 @@ class MessagesStore(object):
|
for old_id, old_symbol in msg.old_names:
|
self._alternative_names[old_id] = msg
|
self._alternative_names[old_symbol] = msg
|
- self._msgs_by_category.setdefault(msg.msgid[0], []).append(msg.msgid)
|
+ self._msgs_by_category[msg.msgid[0]].append(msg.msgid)
|
|
def check_message_id(self, msgid):
|
"""returns the Message object for this message.
|
@@ -615,21 +671,21 @@ class MessagesStore(object):
|
"""display help messages for the given message identifiers"""
|
for msgid in msgids:
|
try:
|
- print self.check_message_id(msgid).format_help(checkerref=True)
|
- print
|
- except UnknownMessage, ex:
|
- print ex
|
- print
|
+ print(self.check_message_id(msgid).format_help(checkerref=True))
|
+ print("")
|
+ except UnknownMessage as ex:
|
+ print(ex)
|
+ print("")
|
continue
|
|
def list_messages(self):
|
"""output full messages list documentation in ReST format"""
|
- msgs = sorted(self._messages.itervalues(), key=lambda msg: msg.msgid)
|
+ msgs = sorted(six.itervalues(self._messages), key=lambda msg: msg.msgid)
|
for msg in msgs:
|
if not msg.may_be_emitted():
|
continue
|
- print msg.format_help(checkerref=False)
|
- print
|
+ print(msg.format_help(checkerref=False))
|
+ print("")
|
|
|
class ReportsHandlerMixIn(object):
|
@@ -637,9 +693,15 @@ class ReportsHandlerMixIn(object):
|
related methods for the main lint class
|
"""
|
def __init__(self):
|
- self._reports = {}
|
+ self._reports = collections.defaultdict(list)
|
self._reports_state = {}
|
|
+ def report_order(self):
|
+ """ Return a list of reports, sorted in the order
|
+ in which they must be called.
|
+ """
|
+ return list(self._reports)
|
+
|
def register_report(self, reportid, r_title, r_cb, checker):
|
"""register a report
|
|
@@ -649,7 +711,7 @@ class ReportsHandlerMixIn(object):
|
checker is the checker defining the report
|
"""
|
reportid = reportid.upper()
|
- self._reports.setdefault(checker, []).append((reportid, r_title, r_cb))
|
+ self._reports[checker].append((reportid, r_title, r_cb))
|
|
def enable_report(self, reportid):
|
"""disable the report of the given id"""
|
@@ -671,7 +733,7 @@ class ReportsHandlerMixIn(object):
|
"""render registered reports"""
|
sect = Section('Report',
|
'%s statements analysed.'% (self.stats['statement']))
|
- for checker in self._reports:
|
+ for checker in self.report_order():
|
for reportid, r_title, r_cb in self._reports[checker]:
|
if not self.report_is_enabled(reportid):
|
continue
|
@@ -688,7 +750,7 @@ class ReportsHandlerMixIn(object):
|
"""add some stats entries to the statistic dictionary
|
raise an AssertionError if there is a key conflict
|
"""
|
- for key, value in kwargs.iteritems():
|
+ for key, value in six.iteritems(kwargs):
|
if key[-1] == '_':
|
key = key[:-1]
|
assert key not in self.stats
|
@@ -721,7 +783,7 @@ def expand_modules(files_or_modules, black_list):
|
if filepath is None:
|
errors.append({'key' : 'ignored-builtin-module', 'mod': modname})
|
continue
|
- except (ImportError, SyntaxError), ex:
|
+ except (ImportError, SyntaxError) as ex:
|
# FIXME p3k : the SyntaxError is a Python bug and should be
|
# removed as soon as possible http://bugs.python.org/issue10588
|
errors.append({'key': 'fatal', 'mod': modname, 'ex': ex})
|
@@ -746,8 +808,8 @@ class PyLintASTWalker(object):
|
def __init__(self, linter):
|
# callbacks per node types
|
self.nbstatements = 1
|
- self.visit_events = {}
|
- self.leave_events = {}
|
+ self.visit_events = collections.defaultdict(list)
|
+ self.leave_events = collections.defaultdict(list)
|
self.linter = linter
|
|
def _is_method_enabled(self, method):
|
@@ -773,20 +835,20 @@ class PyLintASTWalker(object):
|
v_meth = getattr(checker, member)
|
# don't use visit_methods with no activated message:
|
if self._is_method_enabled(v_meth):
|
- visits.setdefault(cid, []).append(v_meth)
|
+ visits[cid].append(v_meth)
|
vcids.add(cid)
|
elif member.startswith('leave_'):
|
l_meth = getattr(checker, member)
|
# don't use leave_methods with no activated message:
|
if self._is_method_enabled(l_meth):
|
- leaves.setdefault(cid, []).append(l_meth)
|
+ leaves[cid].append(l_meth)
|
lcids.add(cid)
|
visit_default = getattr(checker, 'visit_default', None)
|
if visit_default:
|
for cls in nodes.ALL_NODE_CLASSES:
|
cid = cls.__name__.lower()
|
if cid not in vcids:
|
- visits.setdefault(cid, []).append(visit_default)
|
+ visits[cid].append(visit_default)
|
# for now we have no "leave_default" method in Pylint
|
|
def walk(self, astroid):
|
@@ -824,8 +886,9 @@ def register_plugins(linter, directory):
|
except ValueError:
|
# empty module name (usually emacs auto-save files)
|
continue
|
- except ImportError, exc:
|
- print >> sys.stderr, "Problem importing module %s: %s" % (filename, exc)
|
+ except ImportError as exc:
|
+ print("Problem importing module %s: %s" % (filename, exc),
|
+ file=sys.stderr)
|
else:
|
if hasattr(module, 'register'):
|
module.register(linter)
|
|