fname stringlengths 63 176 | rel_fname stringclasses 706
values | line int64 -1 4.5k | name stringlengths 1 81 | kind stringclasses 2
values | category stringclasses 2
values | info stringlengths 0 77.9k ⌀ |
|---|---|---|---|---|---|---|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 113 | basename | ref | function | and os.path.basename(filepath) == "__init__.py"
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 116 | get_module_files | ref | function | for subfilepath in modutils.get_module_files(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 117 | dirname | ref | function | os.path.dirname(filepath), ignore_list, list_all=is_namespace
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 121 | _is_in_ignore_list_re | ref | function | if _is_in_ignore_list_re(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 122 | basename | ref | function | os.path.basename(subfilepath), ignore_list_re
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 123 | _is_in_ignore_list_re | ref | function | ) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/expand_modules.py | pylint/lint/expand_modules.py | 126 | _modpath_from_file | ref | function | modpath = _modpath_from_file(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 38 | _get_new_args | def | function | def _get_new_args(message):
location = (
message.abspath,
message.path,
message.module,
message.obj,
message.line,
message.column,
)
return (message.msg_id, message.symbol, location, message.msg, message.confidence)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 50 | _worker_initialize | def | function | def _worker_initialize(
linter: bytes, arguments: Union[None, str, Sequence[str]] = None
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 63 | set_reporter | ref | function | _worker_linter.set_reporter(reporters.CollectingReporter())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 63 | CollectingReporter | ref | function | _worker_linter.set_reporter(reporters.CollectingReporter())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 67 | _patch_sys_path | ref | function | _patch_sys_path(arguments or ())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 70 | _worker_check_single_file | def | function | def _worker_check_single_file(
file_item: FileItem,
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 78 | check_single_file_item | ref | function | _worker_linter.check_single_file_item(file_item)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 80 | get_checkers | ref | function | for checker in _worker_linter.get_checkers():
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 82 | get_map_data | ref | function | data = checker.get_map_data()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 86 | _get_new_args | ref | function | msgs = [_get_new_args(m) for m in _worker_linter.reporter.messages]
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 87 | reset | ref | function | _worker_linter.reporter.reset()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 108 | _merge_mapreduce_data | def | function | def _merge_mapreduce_data(linter, all_mapreduce_data):
"""Merges map/reduce data across workers, invoking relevant APIs on checkers."""
# First collate the data and prepare it, so we can send it to the checkers for
# validation. The intent here is to collect all the mapreduce data for all checker-
# runs across processes - that will then be passed to a static method on the
# checkers to be reduced and further processed.
collated_map_reduce_data = collections.defaultdict(list)
for linter_data in all_mapreduce_data.values():
for run_data in linter_data:
for checker_name, data in run_data.items():
collated_map_reduce_data[checker_name].extend(data)
# Send the data to checkers that support/require consolidated data
original_checkers = linter.get_checkers()
for checker in original_checkers:
if checker.name in collated_map_reduce_data:
# Assume that if the check has returned map/reduce data that it has the
# reducer function
checker.reduce_map_data(linter, collated_map_reduce_data[checker.name])
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 121 | get_checkers | ref | function | original_checkers = linter.get_checkers()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 126 | reduce_map_data | ref | function | checker.reduce_map_data(linter, collated_map_reduce_data[checker.name])
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 129 | check_parallel | def | function | def check_parallel(
linter: "PyLinter",
jobs: int,
files: Iterable[FileItem],
arguments: Union[None, str, Sequence[str]] = None,
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 163 | imap_unordered | ref | function | ) in pool.imap_unordered(_worker_check_single_file, files):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 165 | set_current_module | ref | function | linter.set_current_module(module, file_path)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 167 | Message | ref | function | msg = Message(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 168 | MessageLocationTuple | ref | function | msg[0], msg[1], MessageLocationTuple(*msg[2]), msg[3], msg[4]
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 170 | handle_message | ref | function | linter.reporter.handle_message(msg)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 175 | _merge_mapreduce_data | ref | function | _merge_mapreduce_data(linter, all_mapreduce_data)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 176 | merge_stats | ref | function | linter.stats = merge_stats([linter.stats] + all_stats)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/parallel.py | pylint/lint/parallel.py | 179 | get_checkers | ref | function | for checker in linter.get_checkers():
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 80 | _read_stdin | def | function | def _read_stdin():
# https://mail.python.org/pipermail/python-list/2012-November/634424.html
sys.stdin = TextIOWrapper(sys.stdin.detach(), encoding="utf-8")
return sys.stdin.read()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 86 | _load_reporter_by_class | def | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 88 | get_module_part | ref | function | module_part = astroid.modutils.get_module_part(qname)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 89 | load_module_from_name | ref | function | module = astroid.modutils.load_module_from_name(module_part)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 190 | PyLinter | def | class | make_options __init__ load_default_plugins load_plugin_modules load_plugin_configuration _load_reporters _load_reporter_by_name set_reporter set_option register_reporter report_order register_checker enable_fail_on_messages any_fail_on_issues disable_noerror_messages disable_reporters error_mode list_messages_enabled process_tokens get_checkers get_checker_names prepare_checkers should_analyze_file initialize _discover_files check check_single_file check_single_file_item _check_files _check_file _get_file_descr_from_stdin _iterate_file_descrs _expand_files set_current_module _astroid_module_checker get_ast check_astroid_module _check_astroid_module open generate_reports _report_evaluation _get_message_state_scope _is_one_message_enabled is_message_enabled _add_one_message add_message add_ignored_message _message_symbol _set_one_msg_status _get_messages_to_set _set_msg_status _register_by_id_managed_msg disable disable_next enable |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 221 | make_options | def | function | def make_options() -> Tuple[Tuple[str, OptionDict], ...]:
return (
(
"ignore",
{
"type": "csv",
"metavar": "<file>[,<file>...]",
"dest": "black_list",
"default": ("CVS",),
"help": "Files or directories to be skipped. "
"They should be base names, not paths.",
},
),
(
"ignore-patterns",
{
"type": "regexp_csv",
"metavar": "<pattern>[,<pattern>...]",
"dest": "black_list_re",
"default": (r"^\.#",),
"help": "Files or directories matching the regex patterns are"
" skipped. The regex matches against base names, not paths. The default value "
"ignores emacs file locks",
},
),
(
"ignore-paths",
{
"type": "regexp_paths_csv",
"metavar": "<pattern>[,<pattern>...]",
"default": [],
"help": "Add files or directories matching the regex patterns to the "
"ignore-list. The regex matches against paths and can be in "
"Posix or Windows format.",
},
),
(
"persistent",
{
"default": _True,
"type": "yn",
"metavar": "<y or n>",
"level": 1,
"help": "Pickle collected data for later comparisons.",
},
),
(
"load-plugins",
{
"type": "csv",
"metavar": "<modules>",
"default": (),
"level": 1,
"help": "List of plugins (as comma separated values of "
"python module names) to load, usually to register "
"additional checkers.",
},
),
(
"output-format",
{
"default": "text",
"type": "string",
"metavar": "<format>",
"short": "f",
"group": "Reports",
"help": "Set the output format. Available formats are text,"
" parseable, colorized, json and msvs (visual studio)."
" You can also give a reporter class, e.g. mypackage.mymodule."
"MyReporterClass.",
},
),
(
"reports",
{
"default": _False,
"type": "yn",
"metavar": "<y or n>",
"short": "r",
"group": "Reports",
"help": "Tells whether to display a full report or only the "
"messages.",
},
),
(
"evaluation",
{
"type": "string",
"metavar": "<python_expression>",
"group": "Reports",
"level": 1,
"default": "max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + "
"convention) / statement) * 10))",
"help": "Python expression which should return a score less "
"than or equal to 10. You have access to the variables 'fatal', "
"'error', 'warning', 'refactor', 'convention', and 'info' which "
"contain the number of messages in each category, as well as "
"'statement' which is the total number of statements "
"analyzed. This score is used by the global "
"evaluation report (RP0004).",
},
),
(
"score",
{
"default": _True,
"type": "yn",
"metavar": "<y or n>",
"short": "s",
"group": "Reports",
"help": "Activate the evaluation score.",
},
),
(
"fail-under",
{
"default": 10,
"type": "float",
"metavar": "<score>",
"help": "Specify a score threshold to be exceeded before program exits with error.",
},
),
(
"fail-on",
{
"default": "",
"type": "csv",
"metavar": "<msg ids>",
"help": "Return non-zero exit code if any of these messages/categories are detected,"
" even if score is above --fail-under value. Syntax same as enable."
" Messages specified are enabled, while categories only check already-enabled messages.",
},
),
(
"confidence",
{
"type": "multiple_choice",
"metavar": "<levels>",
"default": "",
"choices": [c.name for c in interfaces.CONFIDENCE_LEVELS],
"group": "Messages control",
"help": "Only show warnings with the listed confidence levels."
f" Leave empty to show all. Valid levels: {', '.join(c.name for c in interfaces.CONFIDENCE_LEVELS)}.",
},
),
(
"enable",
{
"type": "csv",
"metavar": "<msg ids>",
"short": "e",
"group": "Messages control",
"help": "Enable the message, report, category or checker with the "
"given id(s). You can either give multiple identifier "
"separated by comma (,) or put this option multiple time "
"(only on the command line, not in the configuration file "
"where it should appear only once). "
'See also the "--disable" option for examples.',
},
),
(
"disable",
{
"type": "csv",
"metavar": "<msg ids>",
"short": "d",
"group": "Messages control",
"help": "Disable the message, report, category or checker "
"with the given id(s). You can either give multiple identifiers "
"separated by comma (,) or put this option multiple times "
"(only on the command line, not in the configuration file "
"where it should appear only once). "
'You can also use "--disable=all" to disable everything first '
"and then re-enable specific checks. For example, if you want "
"to run only the similarities checker, you can use "
'"--disable=all --enable=similarities". '
"If you want to run only the classes checker, but have no "
"Warning level messages displayed, use "
'"--disable=all --enable=classes --disable=W".',
},
),
(
"msg-template",
{
"type": "string",
"metavar": "<template>",
"group": "Reports",
"help": (
"Template used to display messages. "
"This is a python new-style format string "
"used to format the message information. "
"See doc for all details."
),
},
),
(
"jobs",
{
"type": "int",
"metavar": "<n-processes>",
"short": "j",
"default": 1,
"help": "Use multiple processes to speed up Pylint. Specifying 0 will "
"auto-detect the number of processors available to use.",
},
),
(
"unsafe-load-any-extension",
{
"type": "yn",
"metavar": "<y or n>",
"default": _False,
"hide": _True,
"help": (
"Allow loading of arbitrary C extensions. Extensions"
" are imported into the active Python interpreter and"
" may run arbitrary code."
),
},
),
(
"limit-inference-results",
{
"type": "int",
"metavar": "<number-of-results>",
"default": 100,
"help": (
"Control the amount of potential inferred values when inferring "
"a single object. This can help the performance when dealing with "
"large functions or complex, nested conditions. "
),
},
),
(
"extension-pkg-allow-list",
{
"type": "csv",
"metavar": "<pkg[,pkg]>",
"default": [],
"help": (
"A comma-separated list of package or module names"
" from where C extensions may be loaded. Extensions are"
" loading into the active Python interpreter and may run"
" arbitrary code."
),
},
),
(
"extension-pkg-whitelist",
{
"type": "csv",
"metavar": "<pkg[,pkg]>",
"default": [],
"help": (
"A comma-separated list of package or module names"
" from where C extensions may be loaded. Extensions are"
" loading into the active Python interpreter and may run"
" arbitrary code. (This is an alternative name to"
" extension-pkg-allow-list for backward compatibility.)"
),
},
),
(
"suggestion-mode",
{
"type": "yn",
"metavar": "<y or n>",
"default": _True,
"help": (
"When enabled, pylint would attempt to guess common "
"misconfiguration and emit user-friendly hints instead "
"of false-positive error messages."
),
},
),
(
"exit-zero",
{
"action": "store_true",
"help": (
"Always return a 0 (non-error) status code, even if "
"lint errors are found. This is primarily useful in "
"continuous integration scripts."
),
},
),
(
"from-stdin",
{
"action": "store_true",
"help": (
"Interpret the stdin as a python script, whose filename "
"needs to be passed as the module_or_package argument."
),
},
),
(
"recursive",
{
"type": "yn",
"metavar": "<yn>",
"default": _False,
"help": "Discover python modules and packages in the file system subtree.",
},
),
(
"py-version",
{
"default": sys.version_info[:2],
"type": "py_version",
"metavar": "<py_version>",
"help": (
"Minimum Python version to use for version dependent checks. "
"Will default to the version used to run pylint."
),
},
),
)
base_option_groups = (
("Messages control", "Options controlling analysis messages"),
("Reports", "Options related to output formatting and reporting"),
)
def __init__(
self,
options: Tuple[Tuple[str, OptionDict], ...] = (),
reporter: Union[reporters.BaseReporter, reporters.MultiReporter, None] = None,
option_groups: Tuple[Tuple[str, str], ...] = (),
pylintrc: Optional[str] = None,
) -> None:
"""Some stuff has to be done before ancestors initialization...
messages store / checkers / reporter / astroid manager
"""
# Attributes for reporters
self.reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
if reporter:
self.set_reporter(reporter)
else:
self.set_reporter(TextReporter())
self._reporters: Dict[str, Type[reporters.BaseReporter]] = {}
"""Dictionary of possible but non-initialized reporters."""
# Attributes for checkers and plugins
self._checkers: DefaultDict[
str, List[checkers.BaseChecker]
] = collections.defaultdict(list)
"""Dictionary of registered and initialized checkers."""
self._dynamic_plugins: Set[str] = set()
"""Set of loaded plugin names."""
# Attributes related to visiting files
self.file_state = FileState()
self.current_name: Optional[str] = None
self.current_file: Optional[str] = None
self._ignore_file = _False
self._pragma_lineno: Dict[str, int] = {}
# Attributes related to stats
self.stats = LinterStats()
# Attributes related to (command-line) options and their parsing
self._external_opts = options
self.options: Tuple[Tuple[str, OptionDict], ...] = (
options + PyLinter.make_options()
)
self.option_groups: Tuple[Tuple[str, str], ...] = (
option_groups + PyLinter.base_option_groups
)
self._options_methods = {
"enable": self.enable,
"disable": self.disable,
"disable-next": self.disable_next,
}
self._bw_options_methods = {
"disable-msg": self._options_methods["disable"],
"enable-msg": self._options_methods["enable"],
}
self.fail_on_symbols: List[str] = []
"""List of message symbols on which pylint should fail, set by --fail-on."""
self._error_mode = _False
# Attributes related to messages (states) and their handling
self.msgs_store = MessageDefinitionStore()
self.msg_status = 0
self._msgs_state: Dict[str, bool] = {}
self._by_id_managed_msgs: List[ManagedMessage] = []
reporters.ReportsHandlerMixIn.__init__(self)
super().__init__(
usage=__doc__,
config_file=pylintrc or next(config.find_default_config_files(), None),
)
checkers.BaseTokenChecker.__init__(self)
# provided reports
self.reports = (
("RP0001", "Messages by category", report_total_messages_stats),
(
"RP0002",
"% errors / warnings by module",
report_messages_by_module_stats,
),
("RP0003", "Messages", report_messages_stats),
)
self.register_checker(self)
self.load_provider_defaults()
def load_default_plugins(self):
checkers.initialize(self)
reporters.initialize(self)
def load_plugin_modules(self, modnames):
"""Take a list of module names which are pylint plugins and load
and register them
"""
for modname in modnames:
if modname in self._dynamic_plugins:
continue
self._dynamic_plugins.add(modname)
try:
module = astroid.modutils.load_module_from_name(modname)
module.register(self)
except ModuleNotFoundError:
pass
def load_plugin_configuration(self):
"""Call the configuration hook for plugins.
This walks through the list of plugins, grabs the "load_configuration"
hook, if exposed, and calls it to allow plugins to configure specific
settings.
"""
for modname in self._dynamic_plugins:
try:
module = astroid.modutils.load_module_from_name(modname)
if hasattr(module, "load_configuration"):
module.load_configuration(self)
except ModuleNotFoundError as e:
self.add_message("bad-plugin-value", args=(modname, e), line=0)
def _load_reporters(self, reporter_names: str) -> None:
"""Load the reporters if they are available on _reporters."""
if not self._reporters:
return
sub_reporters = []
output_files = []
with contextlib.ExitStack() as stack:
for reporter_name in reporter_names.split(","):
reporter_name, *reporter_output = reporter_name.split(":", 1)
reporter = self._load_reporter_by_name(reporter_name)
sub_reporters.append(reporter)
if reporter_output:
output_file = stack.enter_context(
open(reporter_output[0], "w", encoding="utf-8")
)
reporter.out = output_file
output_files.append(output_file)
# Extend the lifetime of all opened output files
close_output_files = stack.pop_all().close
if len(sub_reporters) > 1 or output_files:
self.set_reporter(
reporters.MultiReporter(
sub_reporters,
close_output_files,
)
)
else:
self.set_reporter(sub_reporters[0])
def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
name = reporter_name.lower()
if name in self._reporters:
return self._reporters[name]()
try:
reporter_class = _load_reporter_by_class(reporter_name)
except (ImportError, AttributeError) as e:
raise exceptions.InvalidReporterError(name) from e
else:
return reporter_class()
def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 558 | set_reporter | ref | function | self.set_reporter(reporter)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 560 | set_reporter | ref | function | self.set_reporter(TextReporter())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 560 | TextReporter | ref | function | self.set_reporter(TextReporter())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 573 | FileState | ref | function | self.file_state = FileState()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 580 | LinterStats | ref | function | self.stats = LinterStats()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 585 | make_options | ref | function | options + PyLinter.make_options()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 604 | MessageDefinitionStore | ref | function | self.msgs_store = MessageDefinitionStore()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 612 | find_default_config_files | ref | function | config_file=pylintrc or next(config.find_default_config_files(), None),
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 625 | register_checker | ref | function | self.register_checker(self)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 626 | load_provider_defaults | ref | function | self.load_provider_defaults()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 628 | load_default_plugins | def | function | def load_default_plugins(self):
checkers.initialize(self)
reporters.initialize(self)
def load_plugin_modules(self, modnames):
"""Take a list of module names which are pylint plugins and load
and register them
"""
for modname in modnames:
if modname in self._dynamic_plugins:
continue
self._dynamic_plugins.add(modname)
try:
module = astroid.modutils.load_module_from_name(modname)
module.register(self)
except ModuleNotFoundError:
pass
def load_plugin_configuration(self):
"""Call the configuration hook for plugins.
This walks through the list of plugins, grabs the "load_configuration"
hook, if exposed, and calls it to allow plugins to configure specific
settings.
"""
for modname in self._dynamic_plugins:
try:
module = astroid.modutils.load_module_from_name(modname)
if hasattr(module, "load_configuration"):
module.load_configuration(self)
except ModuleNotFoundError as e:
self.add_message("bad-plugin-value", args=(modname, e), line=0)
def _load_reporters(self, reporter_names: str) -> None:
"""Load the reporters if they are available on _reporters."""
if not self._reporters:
return
sub_reporters = []
output_files = []
with contextlib.ExitStack() as stack:
for reporter_name in reporter_names.split(","):
reporter_name, *reporter_output = reporter_name.split(":", 1)
reporter = self._load_reporter_by_name(reporter_name)
sub_reporters.append(reporter)
if reporter_output:
output_file = stack.enter_context(
open(reporter_output[0], "w", encoding="utf-8")
)
reporter.out = output_file
output_files.append(output_file)
# Extend the lifetime of all opened output files
close_output_files = stack.pop_all().close
if len(sub_reporters) > 1 or output_files:
self.set_reporter(
reporters.MultiReporter(
sub_reporters,
close_output_files,
)
)
else:
self.set_reporter(sub_reporters[0])
def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
name = reporter_name.lower()
if name in self._reporters:
return self._reporters[name]()
try:
reporter_class = _load_reporter_by_class(reporter_name)
except (ImportError, AttributeError) as e:
raise exceptions.InvalidReporterError(name) from e
else:
return reporter_class()
def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 629 | initialize | ref | function | checkers.initialize(self)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 630 | initialize | ref | function | reporters.initialize(self)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 632 | load_plugin_modules | def | function | def load_plugin_modules(self, modnames):
"""Take a list of module names which are pylint plugins and load
and register them
"""
for modname in modnames:
if modname in self._dynamic_plugins:
continue
self._dynamic_plugins.add(modname)
try:
module = astroid.modutils.load_module_from_name(modname)
module.register(self)
except ModuleNotFoundError:
pass
def load_plugin_configuration(self):
"""Call the configuration hook for plugins.
This walks through the list of plugins, grabs the "load_configuration"
hook, if exposed, and calls it to allow plugins to configure specific
settings.
"""
for modname in self._dynamic_plugins:
try:
module = astroid.modutils.load_module_from_name(modname)
if hasattr(module, "load_configuration"):
module.load_configuration(self)
except ModuleNotFoundError as e:
self.add_message("bad-plugin-value", args=(modname, e), line=0)
def _load_reporters(self, reporter_names: str) -> None:
"""Load the reporters if they are available on _reporters."""
if not self._reporters:
return
sub_reporters = []
output_files = []
with contextlib.ExitStack() as stack:
for reporter_name in reporter_names.split(","):
reporter_name, *reporter_output = reporter_name.split(":", 1)
reporter = self._load_reporter_by_name(reporter_name)
sub_reporters.append(reporter)
if reporter_output:
output_file = stack.enter_context(
open(reporter_output[0], "w", encoding="utf-8")
)
reporter.out = output_file
output_files.append(output_file)
# Extend the lifetime of all opened output files
close_output_files = stack.pop_all().close
if len(sub_reporters) > 1 or output_files:
self.set_reporter(
reporters.MultiReporter(
sub_reporters,
close_output_files,
)
)
else:
self.set_reporter(sub_reporters[0])
def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
name = reporter_name.lower()
if name in self._reporters:
return self._reporters[name]()
try:
reporter_class = _load_reporter_by_class(reporter_name)
except (ImportError, AttributeError) as e:
raise exceptions.InvalidReporterError(name) from e
else:
return reporter_class()
def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 641 | load_module_from_name | ref | function | module = astroid.modutils.load_module_from_name(modname)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 642 | register | ref | function | module.register(self)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 646 | load_plugin_configuration | def | function | def load_plugin_configuration(self):
"""Call the configuration hook for plugins.
This walks through the list of plugins, grabs the "load_configuration"
hook, if exposed, and calls it to allow plugins to configure specific
settings.
"""
for modname in self._dynamic_plugins:
try:
module = astroid.modutils.load_module_from_name(modname)
if hasattr(module, "load_configuration"):
module.load_configuration(self)
except ModuleNotFoundError as e:
self.add_message("bad-plugin-value", args=(modname, e), line=0)
def _load_reporters(self, reporter_names: str) -> None:
"""Load the reporters if they are available on _reporters."""
if not self._reporters:
return
sub_reporters = []
output_files = []
with contextlib.ExitStack() as stack:
for reporter_name in reporter_names.split(","):
reporter_name, *reporter_output = reporter_name.split(":", 1)
reporter = self._load_reporter_by_name(reporter_name)
sub_reporters.append(reporter)
if reporter_output:
output_file = stack.enter_context(
open(reporter_output[0], "w", encoding="utf-8")
)
reporter.out = output_file
output_files.append(output_file)
# Extend the lifetime of all opened output files
close_output_files = stack.pop_all().close
if len(sub_reporters) > 1 or output_files:
self.set_reporter(
reporters.MultiReporter(
sub_reporters,
close_output_files,
)
)
else:
self.set_reporter(sub_reporters[0])
def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
name = reporter_name.lower()
if name in self._reporters:
return self._reporters[name]()
try:
reporter_class = _load_reporter_by_class(reporter_name)
except (ImportError, AttributeError) as e:
raise exceptions.InvalidReporterError(name) from e
else:
return reporter_class()
def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 655 | load_module_from_name | ref | function | module = astroid.modutils.load_module_from_name(modname)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 657 | load_configuration | ref | function | module.load_configuration(self)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 659 | add_message | ref | function | self.add_message("bad-plugin-value", args=(modname, e), line=0)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 661 | _load_reporters | def | function | def _load_reporters(self, reporter_names: str) -> None:
"""Load the reporters if they are available on _reporters."""
if not self._reporters:
return
sub_reporters = []
output_files = []
with contextlib.ExitStack() as stack:
for reporter_name in reporter_names.split(","):
reporter_name, *reporter_output = reporter_name.split(":", 1)
reporter = self._load_reporter_by_name(reporter_name)
sub_reporters.append(reporter)
if reporter_output:
output_file = stack.enter_context(
open(reporter_output[0], "w", encoding="utf-8")
)
reporter.out = output_file
output_files.append(output_file)
# Extend the lifetime of all opened output files
close_output_files = stack.pop_all().close
if len(sub_reporters) > 1 or output_files:
self.set_reporter(
reporters.MultiReporter(
sub_reporters,
close_output_files,
)
)
else:
self.set_reporter(sub_reporters[0])
def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
name = reporter_name.lower()
if name in self._reporters:
return self._reporters[name]()
try:
reporter_class = _load_reporter_by_class(reporter_name)
except (ImportError, AttributeError) as e:
raise exceptions.InvalidReporterError(name) from e
else:
return reporter_class()
def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 671 | _load_reporter_by_name | ref | function | reporter = self._load_reporter_by_name(reporter_name)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 674 | enter_context | ref | function | output_file = stack.enter_context(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 681 | pop_all | ref | function | close_output_files = stack.pop_all().close
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 684 | set_reporter | ref | function | self.set_reporter(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 685 | MultiReporter | ref | function | reporters.MultiReporter(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 691 | set_reporter | ref | function | self.set_reporter(sub_reporters[0])
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 693 | _load_reporter_by_name | def | function | def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
name = reporter_name.lower()
if name in self._reporters:
return self._reporters[name]()
try:
reporter_class = _load_reporter_by_class(reporter_name)
except (ImportError, AttributeError) as e:
raise exceptions.InvalidReporterError(name) from e
else:
return reporter_class()
def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 699 | _load_reporter_by_class | ref | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 701 | InvalidReporterError | ref | function | raise exceptions.InvalidReporterError(name) from e
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 703 | reporter_class | ref | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 705 | set_reporter | def | function | def set_reporter(
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
) -> None:
"""Set the reporter used to display messages and reports."""
self.reporter = reporter
reporter.linter = self
def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 712 | set_option | def | function | def set_option(self, optname, value, action=None, optdict=None):
"""Overridden from config.OptionsProviderMixin to handle some
special options
"""
if optname in self._options_methods or optname in self._bw_options_methods:
if value:
try:
meth = self._options_methods[optname]
except KeyError:
meth = self._bw_options_methods[optname]
warnings.warn(
f"{optname} is deprecated, replace it by {optname.split('-')[0]}",
DeprecationWarning,
)
value = utils._check_csv(value)
if isinstance(value, (list, tuple)):
for _id in value:
meth(_id, ignore_unknown=_True)
else:
meth(value)
return # no need to call set_option, disable/enable methods do it
elif optname == "output-format":
assert isinstance(
value, str
), "'output-format' should be a comma separated string of reporters"
self._load_reporters(value)
try:
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
except config.UnsupportedAction:
print(f"option {optname} can't be read from config file", file=sys.stderr)
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
"""Registers a reporter class on the _reporters attribute."""
self._reporters[reporter_class.name] = reporter_class
def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 726 | _check_csv | ref | function | value = utils._check_csv(value)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 729 | meth | ref | function | meth(_id, ignore_unknown=True)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 731 | meth | ref | function | meth(value)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 737 | _load_reporters | ref | function | self._load_reporters(value)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 739 | set_option | ref | function | checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 743 | register_reporter | def | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 747 | report_order | def | function | def report_order(self):
reports = sorted(self._reports, key=lambda x: getattr(x, "name", ""))
try:
# Remove the current reporter and add it
# at the end of the list.
reports.pop(reports.index(self))
except ValueError:
pass
else:
reports.append(self)
return reports
# checkers manipulation methods ############################################
def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 761 | register_checker | def | function | def register_checker(self, checker: checkers.BaseChecker) -> None:
"""This method auto registers the checker."""
assert checker.priority <= 0, "checker priority can't be >= 0"
self._checkers[checker.name].append(checker)
for r_id, r_title, r_cb in checker.reports:
self.register_report(r_id, r_title, r_cb, checker)
self.register_options_provider(checker)
if hasattr(checker, "msgs"):
self.msgs_store.register_messages_from_checker(checker)
checker.load_defaults()
# Register the checker, but disable all of its messages.
if not getattr(checker, "enabled", _True):
self.disable(checker.name)
def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 766 | register_report | ref | function | self.register_report(r_id, r_title, r_cb, checker)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 767 | register_options_provider | ref | function | self.register_options_provider(checker)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 769 | register_messages_from_checker | ref | function | self.msgs_store.register_messages_from_checker(checker)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 770 | load_defaults | ref | function | checker.load_defaults()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 773 | disable | ref | function | self.disable(checker.name)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 775 | enable_fail_on_messages | def | function | def enable_fail_on_messages(self):
"""Enable 'fail on' msgs.
Convert values in config.fail_on (which might be msg category, msg id,
or symbol) to specific msgs, then enable and flag them for later.
"""
fail_on_vals = self.config.fail_on
if not fail_on_vals:
return
fail_on_cats = set()
fail_on_msgs = set()
for val in fail_on_vals:
# If value is a category, add category, else add message
if val in MSG_TYPES:
fail_on_cats.add(val)
else:
fail_on_msgs.add(val)
# For every message in every checker, if cat or msg flagged, enable check
for all_checkers in self._checkers.values():
for checker in all_checkers:
for msg in checker.messages:
if msg.msgid in fail_on_msgs or msg.symbol in fail_on_msgs:
# message id/symbol matched, enable and flag it
self.enable(msg.msgid)
self.fail_on_symbols.append(msg.symbol)
elif msg.msgid[0] in fail_on_cats:
# message starts with a category value, flag (but do not enable) it
self.fail_on_symbols.append(msg.symbol)
def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 800 | enable | ref | function | self.enable(msg.msgid)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 806 | any_fail_on_issues | def | function | def any_fail_on_issues(self):
return self.stats and any(
x in self.fail_on_symbols for x in self.stats.by_msg.keys()
)
def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 811 | disable_noerror_messages | def | function | def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
# enable only messages with 'error' severity and above ('fatal')
if msgcat in {"E", "F"}:
for msgid in msgids:
self.enable(msgid)
else:
for msgid in msgids:
self.disable(msgid)
def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 816 | enable | ref | function | self.enable(msgid)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 819 | disable | ref | function | self.disable(msgid)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 821 | disable_reporters | def | function | def disable_reporters(self):
"""Disable all reporters."""
for _reporters in self._reports.values():
for report_id, _, _ in _reporters:
self.disable_report(report_id)
def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 825 | disable_report | ref | function | self.disable_report(report_id)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 827 | error_mode | def | function | def error_mode(self):
"""Error mode: enable only errors; no reports, no persistent."""
self._error_mode = _True
self.disable_noerror_messages()
self.disable("miscellaneous")
self.set_option("reports", _False)
self.set_option("persistent", _False)
self.set_option("score", _False)
def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 830 | disable_noerror_messages | ref | function | self.disable_noerror_messages()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 831 | disable | ref | function | self.disable("miscellaneous")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 832 | set_option | ref | function | self.set_option("reports", False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 833 | set_option | ref | function | self.set_option("persistent", False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 834 | set_option | ref | function | self.set_option("score", False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 836 | list_messages_enabled | def | function | def list_messages_enabled(self):
emittable, non_emittable = self.msgs_store.find_emittable_messages()
enabled = []
disabled = []
for message in emittable:
if self.is_message_enabled(message.msgid):
enabled.append(f" {message.symbol} ({message.msgid})")
else:
disabled.append(f" {message.symbol} ({message.msgid})")
print("Enabled messages:")
for msg in enabled:
print(msg)
print("\nDisabled messages:")
for msg in disabled:
print(msg)
print("\nNon-emittable messages with current interpreter:")
for msg in non_emittable:
print(f" {msg.symbol} ({msg.msgid})")
print("")
# block level option handling #############################################
# see func_block_disable_msg.py test case for expected behaviour
def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 837 | find_emittable_messages | ref | function | emittable, non_emittable = self.msgs_store.find_emittable_messages()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 841 | is_message_enabled | ref | function | if self.is_message_enabled(message.msgid):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/lint/pylinter.py | pylint/lint/pylinter.py | 859 | process_tokens | def | function | def process_tokens(self, tokens):
"""Process tokens from the current module to search for module/block level
options.
"""
control_pragmas = {"disable", "disable-next", "enable"}
prev_line = None
saw_newline = _True
seen_newline = _True
for (tok_type, content, start, _, _) in tokens:
if prev_line and prev_line != start[0]:
saw_newline = seen_newline
seen_newline = _False
prev_line = start[0]
if tok_type in (tokenize.NL, tokenize.NEWLINE):
seen_newline = _True
if tok_type != tokenize.COMMENT:
continue
match = OPTION_PO.search(content)
if match is None:
continue
try:
for pragma_repr in parse_pragma(match.group(2)):
if pragma_repr.action in {"disable-all", "skip-file"}:
if pragma_repr.action == "disable-all":
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable-all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
try:
meth = self._options_methods[pragma_repr.action]
except KeyError:
meth = self._bw_options_methods[pragma_repr.action]
# found a "(dis|en)able-msg" pragma deprecated suppression
self.add_message(
"deprecated-pragma",
line=start[0],
args=(
pragma_repr.action,
pragma_repr.action.replace("-msg", ""),
),
)
for msgid in pragma_repr.messages:
# Add the line where a control pragma was encountered.
if pragma_repr.action in control_pragmas:
self._pragma_lineno[msgid] = start[0]
if (pragma_repr.action, msgid) == ("disable", "all"):
self.add_message(
"deprecated-pragma",
line=start[0],
args=("disable=all", "skip-file"),
)
self.add_message("file-ignored", line=start[0])
self._ignore_file = _True
return
# If we did not see a newline between the previous line and now,
# we saw a backslash so treat the two lines as one.
l_start = start[0]
if not saw_newline:
l_start -= 1
try:
meth(msgid, "module", l_start)
except exceptions.UnknownMessageError:
self.add_message(
"bad-option-value", args=msgid, line=start[0]
)
except UnRecognizedOptionError as err:
self.add_message(
"unrecognized-inline-option", args=err.token, line=start[0]
)
continue
except InvalidPragmaError as err:
self.add_message("bad-inline-option", args=err.token, line=start[0])
continue
# code checking methods ###################################################
def get_checkers(self):
"""Return all available checkers as a list."""
return [self] + [
c
for _checkers in self._checkers.values()
for c in _checkers
if c is not self
]
def get_checker_names(self):
"""Get all the checker names that this linter knows about."""
current_checkers = self.get_checkers()
return sorted(
{
checker.name
for checker in current_checkers
if checker.name != MAIN_CHECKER_NAME
}
)
def prepare_checkers(self):
"""Return checkers needed for activated messages and reports."""
if not self.config.reports:
self.disable_reporters()
# get needed checkers
needed_checkers = [self]
for checker in self.get_checkers()[1:]:
messages = {msg for msg in checker.msgs if self.is_message_enabled(msg)}
if messages or any(self.report_is_enabled(r[0]) for r in checker.reports):
needed_checkers.append(checker)
# Sort checkers by priority
needed_checkers = sorted(
needed_checkers, key=operator.attrgetter("priority"), reverse=_True
)
return needed_checkers
# pylint: disable=unused-argument
@staticmethod
def should_analyze_file(modname, path, is_argument=_False):
"""Returns whether a module should be checked.
This implementation returns _True for all python source file, indicating
that all files should be linted.
Subclasses may override this method to indicate that modules satisfying
certain conditions should not be linted.
:param str modname: The name of the module to be checked.
:param str path: The full path to the source code of the module.
:param bool is_argument: Whether the file is an argument to pylint or not.
Files which respect this property are always
checked, since the user requested it explicitly.
:returns: _True if the module should be checked.
:rtype: bool
"""
if is_argument:
return _True
return path.endswith(".py")
# pylint: enable=unused-argument
def initialize(self):
"""Initialize linter for linting.
This method is called before any linting is done.
"""
# initialize msgs_state now that all messages have been registered into
# the store
for msg in self.msgs_store.messages:
if not msg.may_be_emitted():
self._msgs_state[msg.msgid] = _False
@staticmethod
def _discover_files(files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in subdirectory.
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: List[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
yield from (
os.path.join(root, file)
for file in files
if file.endswith(".py")
)
else:
yield something
def check(self, files_or_modules: Union[Sequence[str], str]) -> None:
"""Main checking entry: check a list of files or modules from their name.
files_or_modules is either a string or list of strings presenting modules to check.
"""
self.initialize()
if not isinstance(files_or_modules, (list, tuple)):
# pylint: disable-next=fixme
# TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
warnings.warn(
"In pylint 3.0, the checkers check function will only accept sequence of string",
DeprecationWarning,
)
files_or_modules = (files_or_modules,) # type: ignore[assignment]
if self.config.recursive:
files_or_modules = tuple(self._discover_files(files_or_modules))
if self.config.from_stdin:
if len(files_or_modules) != 1:
raise exceptions.InvalidArgsError(
"Missing filename required for --from-stdin"
)
filepath = files_or_modules[0]
with fix_import_path(files_or_modules):
self._check_files(
functools.partial(self.get_ast, data=_read_stdin()),
[self._get_file_descr_from_stdin(filepath)],
)
elif self.config.jobs == 1:
with fix_import_path(files_or_modules):
self._check_files(
self.get_ast, self._iterate_file_descrs(files_or_modules)
)
else:
check_parallel(
self,
self.config.jobs,
self._iterate_file_descrs(files_or_modules),
files_or_modules,
)
def check_single_file(self, name: str, filepath: str, modname: str) -> None:
warnings.warn(
"In pylint 3.0, the checkers check_single_file function will be removed. "
"Use check_single_file_item instead.",
DeprecationWarning,
)
self.check_single_file_item(FileItem(name, filepath, modname))
def check_single_file_item(self, file: FileItem) -> None:
"""Check single file item.
The arguments are the same that are documented in _check_files
initialize() should be called before calling this method
"""
with self._astroid_module_checker() as check_astroid_module:
self._check_file(self.get_ast, check_astroid_module, file)
def _check_files(
self,
get_ast,
file_descrs: Iterable[FileItem],
) -> None:
"""Check all files from file_descrs."""
with self._astroid_module_checker() as check_astroid_module:
for file in file_descrs:
try:
self._check_file(get_ast, check_astroid_module, file)
except Exception as ex: # pylint: disable=broad-except
template_path = prepare_crash_report(
ex, file.filepath, self.crash_file_path
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
def _check_file(self, get_ast, check_astroid_module, file: FileItem):
"""Check a file using the passed utility functions (get_ast and check_astroid_module).
:param callable get_ast: callable returning AST from defined file taking the following arguments
- filepath: path to the file to check
- name: Python module name
:param callable check_astroid_module: callable checking an AST taking the following arguments
- ast: AST of the module
:param FileItem file: data about the file
"""
self.set_current_module(file.name, file.filepath)
# get the module representation
ast_node = get_ast(file.filepath, file.name)
if ast_node is None:
return
self._ignore_file = _False
self.file_state = FileState(file.modpath)
# fix the current file (if the source file was not available or
# if it's actually a c extension)
self.current_file = ast_node.file
check_astroid_module(ast_node)
# warn about spurious inline messages handling
spurious_messages = self.file_state.iter_spurious_suppression_messages(
self.msgs_store
)
for msgid, line, args in spurious_messages:
self.add_message(msgid, line, None, args)
@staticmethod
def _get_file_descr_from_stdin(filepath: str) -> FileItem:
"""Return file description (tuple of module name, file path, base name) from given file path.
This method is used for creating suitable file description for _check_files when the
source is standard input.
"""
try:
# Note that this function does not really perform an
# __import__ but may raise an ImportError exception, which
# we want to catch here.
modname = ".".join(astroid.modutils.modpath_from_file(filepath))
except ImportError:
modname = os.path.splitext(os.path.basename(filepath))[0]
return FileItem(modname, filepath, filepath)
def _iterate_file_descrs(self, files_or_modules) -> Iterator[FileItem]:
"""Return generator yielding file descriptions (tuples of module name, file path, base name).
The returned generator yield one item for each Python module that should be linted.
"""
for descr in self._expand_files(files_or_modules):
name, filepath, is_arg = descr["name"], descr["path"], descr["isarg"]
if self.should_analyze_file(name, filepath, is_argument=is_arg):
yield FileItem(name, filepath, descr["basename"])
def _expand_files(self, modules) -> List[ModuleDescriptionDict]:
"""Get modules and errors from a list of modules and handle errors."""
result, errors = expand_modules(
modules,
self.config.black_list,
self.config.black_list_re,
self._ignore_paths,
)
for error in errors:
message = modname = error["mod"]
key = error["key"]
self.set_current_module(modname)
if key == "fatal":
message = str(error["ex"]).replace(os.getcwd() + os.sep, "")
self.add_message(key, args=message)
return result
def set_current_module(self, modname, filepath: Optional[str] = None):
"""Set the name of the currently analyzed module and
init statistics for it
"""
if not modname and filepath is None:
return
self.reporter.on_set_current_module(modname, filepath)
if modname is None:
warnings.warn(
(
"In pylint 3.0 modname should be a string so that it can be used to "
"correctly set the current_name attribute of the linter instance. "
"If unknown it should be initialized as an empty string."
),
DeprecationWarning,
)
self.current_name = modname
self.current_file = filepath or modname
self.stats.init_single_module(modname)
@contextlib.contextmanager
def _astroid_module_checker(self):
"""Context manager for checking ASTs.
The value in the context is callable accepting AST as its only argument.
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)
yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)
# notify global end
self.stats.statement = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
def get_ast(
self, filepath: str, modname: str, data: Optional[str] = None
) -> nodes.Module:
"""Return an ast(roid) representation of a module or a string.
:param str filepath: path to checked file.
:param str modname: The name of the module to be checked.
:param str data: optional contents of the checked file.
:returns: the AST
:rtype: astroid.nodes.Module
:raises AstroidBuildingError: Whenever we encounter an unexpected exception
"""
try:
if data is None:
return MANAGER.ast_from_file(filepath, modname, source=_True)
return astroid.builder.AstroidBuilder(MANAGER).string_build(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
except Exception as ex:
traceback.print_exc()
# We raise BuildingError here as this is essentially an astroid issue
# Creating an issue template and adding the 'astroid-error' message is handled
# by caller: _check_files
raise astroid.AstroidBuildingError(
"Building error when trying to create ast representation of module '{modname}'",
modname=modname,
) from ex
return None
def check_astroid_module(self, ast_node, walker, rawcheckers, tokencheckers):
"""Check a module from its astroid representation.
For return value see _check_astroid_module
"""
before_check_statements = walker.nbstatements
retval = self._check_astroid_module(
ast_node, walker, rawcheckers, tokencheckers
)
self.stats.by_module[self.current_name]["statement"] = (
walker.nbstatements - before_check_statements
)
return retval
def _check_astroid_module(
self, node: nodes.Module, walker, rawcheckers, tokencheckers
):
"""Check given AST node with given walker and checkers.
:param astroid.nodes.Module node: AST node of the module to check
:param pylint.utils.ast_walker.ASTWalker walker: AST walker
:param list rawcheckers: List of token checkers to use
:param list tokencheckers: List of raw checkers to use
:returns: _True if the module was checked, _False if ignored,
None if the module contents could not be parsed
:rtype: bool
"""
try:
tokens = utils.tokenize_module(node)
except tokenize.TokenError as ex:
self.add_message("syntax-error", line=ex.args[1][0], args=ex.args[0])
return None
if not node.pure_python:
self.add_message("raw-checker-failed", args=node.name)
else:
# assert astroid.file.endswith('.py')
# invoke ITokenChecker interface on self to fetch module/block
# level options
self.process_tokens(tokens)
if self._ignore_file:
return _False
# walk ast to collect line numbers
self.file_state.collect_block_lines(self.msgs_store, node)
# run raw and tokens checkers
for checker in rawcheckers:
checker.process_module(node)
for checker in tokencheckers:
checker.process_tokens(tokens)
# generate events to astroid checkers
walker.walk(node)
return _True
# IAstroidChecker interface #################################################
def open(self):
"""Initialize counters."""
self.stats = LinterStats()
MANAGER.always_load_extensions = self.config.unsafe_load_any_extension
MANAGER.max_inferable_values = self.config.limit_inference_results
MANAGER.extension_package_whitelist.update(self.config.extension_pkg_allow_list)
if self.config.extension_pkg_whitelist:
MANAGER.extension_package_whitelist.update(
self.config.extension_pkg_whitelist
)
self.stats.reset_message_count()
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
"""Close the whole package /module, it's time to make reports !
if persistent run, pickle results for later comparison
"""
# Display whatever messages are left on the reporter.
self.reporter.display_messages(report_nodes.Section())
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
self.reporter.on_close(self.stats, previous_stats)
if self.config.reports:
sect = self.make_reports(self.stats, previous_stats)
else:
sect = report_nodes.Section()
if self.config.reports:
self.reporter.display_reports(sect)
score_value = self._report_evaluation()
# save results if persistent run
if self.config.persistent:
config.save_results(self.stats, self.file_state.base_name)
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
return score_value
def _report_evaluation(self):
"""Make the global evaluation report."""
# check with at least check 1 statements (usually 0 when there is a
# syntax error preventing pylint from further processing)
note = None
previous_stats = config.load_results(self.file_state.base_name)
if self.stats.statement == 0:
return note
# get a global note for the code
evaluation = self.config.evaluation
try:
stats_dict = {
"fatal": self.stats.fatal,
"error": self.stats.error,
"warning": self.stats.warning,
"refactor": self.stats.refactor,
"convention": self.stats.convention,
"statement": self.stats.statement,
"info": self.stats.info,
}
note = eval(evaluation, {}, stats_dict) # pylint: disable=eval-used
except Exception as ex: # pylint: disable=broad-except
msg = f"An exception occurred while rating: {ex}"
else:
self.stats.global_note = note
msg = f"Your code has been rated at {note:.2f}/10"
if previous_stats:
pnote = previous_stats.global_note
if pnote is not None:
msg += f" (previous run: {pnote:.2f}/10, {note - pnote:+.2f})"
if self.config.score:
sect = report_nodes.EvaluationSection(msg)
self.reporter.display_reports(sect)
return note
# Adding (ignored) messages to the Message Reporter
def _get_message_state_scope(
self,
msgid: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> Optional[Literal[0, 1, 2]]:
"""Returns the scope at which a message was enabled/disabled."""
if confidence is None:
confidence = interfaces.UNDEFINED
if self.config.confidence and confidence.name not in self.config.confidence:
return MSG_STATE_CONFIDENCE # type: ignore[return-value] # mypy does not infer Literal correctly
try:
if line in self.file_state._module_msgs_state[msgid]:
return MSG_STATE_SCOPE_MODULE # type: ignore[return-value]
except (KeyError, TypeError):
return MSG_STATE_SCOPE_CONFIG # type: ignore[return-value]
return None
def _is_one_message_enabled(self, msgid: str, line: Optional[int]) -> bool:
"""Checks state of a single message for the current file.
This function can't be cached as it depends on self.file_state which can
change.
"""
if line is None:
return self._msgs_state.get(msgid, _True)
try:
return self.file_state._module_msgs_state[msgid][line]
except KeyError:
# Check if the message's line is after the maximum line existing in ast tree.
# This line won't appear in the ast tree and won't be referred in
# self.file_state._module_msgs_state
# This happens for example with a commented line at the end of a module.
max_line_number = self.file_state.get_effective_max_line_number()
if max_line_number and line > max_line_number:
fallback = _True
lines = self.file_state._raw_module_msgs_state.get(msgid, {})
# Doesn't consider scopes, as a 'disable' can be in a
# different scope than that of the current line.
closest_lines = reversed(
[
(message_line, enable)
for message_line, enable in lines.items()
if message_line <= line
]
)
_, fallback_iter = next(closest_lines, (None, None))
if fallback_iter is not None:
fallback = fallback_iter
return self._msgs_state.get(msgid, fallback)
return self._msgs_state.get(msgid, _True)
def is_message_enabled(
self,
msg_descr: str,
line: Optional[int] = None,
confidence: Optional[interfaces.Confidence] = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and confidence level.
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
:param confidence: The confidence of the message
"""
if self.config.confidence and confidence:
if confidence.name not in self.config.confidence:
return _False
try:
msgids = self.msgs_store.message_id_store.get_active_msgids(msg_descr)
except exceptions.UnknownMessageError:
# The linter checks for messages that are not registered
# due to version mismatch, just treat them as message IDs
# for now.
msgids = [msg_descr]
return any(self._is_one_message_enabled(msgid, line) for msgid in msgids)
def _add_one_message(
self,
message_definition: MessageDefinition,
line: Optional[int],
node: Optional[nodes.NodeNG],
args: Optional[Any],
confidence: Optional[interfaces.Confidence],
col_offset: Optional[int],
end_lineno: Optional[int],
end_col_offset: Optional[int],
) -> None:
"""After various checks have passed a single Message is
passed to the reporter and added to stats
"""
message_definition.check_message_definition(line, node)
# Look up "location" data of node if not yet supplied
if node:
if not line:
line = node.fromlineno
if not col_offset:
col_offset = node.col_offset
if not end_lineno:
end_lineno = node.end_lineno
if not end_col_offset:
end_col_offset = node.end_col_offset
# should this message be displayed
if not self.is_message_enabled(message_definition.msgid, line, confidence):
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
return
# update stats
msg_cat = MSG_TYPES[message_definition.msgid[0]]
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
self.stats.increase_single_message_count(msg_cat, 1)
self.stats.increase_single_module_message_count(
self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580
msg_cat,
1,
)
try:
self.stats.by_msg[message_definition.symbol] += 1
except KeyError:
self.stats.by_msg[message_definition.symbol] = 1
# Interpolate arguments into message string
msg = message_definition.msg
if args:
msg %= args
# get module and object
if node is None:
module, obj = self.current_name, ""
abspath = self.current_file
else:
module, obj = utils.get_module_and_frameid(node)
abspath = node.root().file
if abspath is not None:
path = abspath.replace(self.reporter.path_strip_prefix, "", 1)
else:
path = "configuration"
# add the message
self.reporter.handle_message(
Message(
message_definition.msgid,
message_definition.symbol,
MessageLocationTuple(
abspath or "",
path,
module or "",
obj,
line or 1,
col_offset or 0,
end_lineno,
end_col_offset,
),
msg,
confidence,
)
)
def add_message(
self,
msgid: str,
line: Optional[int] = None,
node: Optional[nodes.NodeNG] = None,
args: Optional[Any] = None,
confidence: Optional[interfaces.Confidence] = None,
col_offset: Optional[int] = None,
end_lineno: Optional[int] = None,
end_col_offset: Optional[int] = None,
) -> None:
"""Adds a message given by ID or name.
If provided, the message string is expanded using args.
AST checkers must provide the node argument (but may optionally
provide line if the line number is different), raw and token checkers
must provide the line argument.
"""
if confidence is None:
confidence = interfaces.UNDEFINED
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
self._add_one_message(
message_definition,
line,
node,
args,
confidence,
col_offset,
end_lineno,
end_col_offset,
)
def add_ignored_message(
self,
msgid: str,
line: int,
node: Optional[nodes.NodeNG] = None,
confidence: Optional[interfaces.Confidence] = interfaces.UNDEFINED,
) -> None:
"""Prepares a message to be added to the ignored message storage.
Some checks return early in special cases and never reach add_message(),
even though they would normally issue a message.
This creates false positives for useless-suppression.
This function avoids this by adding those message to the ignored msgs attribute
"""
message_definitions = self.msgs_store.get_message_definitions(msgid)
for message_definition in message_definitions:
message_definition.check_message_definition(line, node)
self.file_state.handle_ignored_message(
self._get_message_state_scope(
message_definition.msgid, line, confidence
),
message_definition.msgid,
line,
)
# Setting the state (disabled/enabled) of messages and registering them
def _message_symbol(self, msgid: str) -> List[str]:
"""Get the message symbol of the given message id.
Return the original message id if the message does not
exist.
"""
try:
return [md.symbol for md in self.msgs_store.get_message_definitions(msgid)]
except exceptions.UnknownMessageError:
return [msgid]
def _set_one_msg_status(
self, scope: str, msg: MessageDefinition, line: Optional[int], enable: bool
) -> None:
"""Set the status of an individual message."""
if scope == "module":
assert isinstance(line, int) # should always be int inside module scope
self.file_state.set_msg_status(msg, line, enable)
if not enable and msg.symbol != "locally-disabled":
self.add_message(
"locally-disabled", line=line, args=(msg.symbol, msg.msgid)
)
else:
msgs = self._msgs_state
msgs[msg.msgid] = enable
def _get_messages_to_set(
self, msgid: str, enable: bool, ignore_unknown: bool = _False
) -> List[MessageDefinition]:
"""Do some tests and find the actual messages of which the status should be set."""
message_definitions = []
if msgid == "all":
for _msgid in MSG_TYPES:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a category?
category_id = msgid.upper()
if category_id not in MSG_TYPES:
category_id_formatted = MSG_TYPES_LONG.get(category_id)
else:
category_id_formatted = category_id
if category_id_formatted is not None:
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is a checker name?
if msgid.lower() in self._checkers:
for checker in self._checkers[msgid.lower()]:
for _msgid in checker.msgs:
message_definitions.extend(
self._get_messages_to_set(_msgid, enable, ignore_unknown)
)
return message_definitions
# msgid is report id?
if msgid.lower().startswith("rp"):
if enable:
self.enable_report(msgid)
else:
self.disable_report(msgid)
return message_definitions
try:
# msgid is a symbolic or numeric msgid.
message_definitions = self.msgs_store.get_message_definitions(msgid)
except exceptions.UnknownMessageError:
if not ignore_unknown:
raise
return message_definitions
def _set_msg_status(
self,
msgid: str,
enable: bool,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Do some tests and then iterate over message definitions to set state."""
assert scope in {"package", "module"}
message_definitions = self._get_messages_to_set(msgid, enable, ignore_unknown)
for message_definition in message_definitions:
self._set_one_msg_status(scope, message_definition, line, enable)
# sync configuration object
self.config.enable = []
self.config.disable = []
for mid, val in self._msgs_state.items():
if val:
self.config.enable.append(self._message_symbol(mid))
else:
self.config.disable.append(self._message_symbol(mid))
def _register_by_id_managed_msg(
self, msgid_or_symbol: str, line: Optional[int], is_disabled: bool = _True
) -> None:
"""If the msgid is a numeric one, then register it to inform the user
it could furnish instead a symbolic msgid.
"""
if msgid_or_symbol[1:].isdigit():
try:
symbol = self.msgs_store.message_id_store.get_symbol(
msgid=msgid_or_symbol
)
except exceptions.UnknownMessageError:
return
managed = ManagedMessage(
self.current_name, msgid_or_symbol, symbol, line, is_disabled
)
self._by_id_managed_msgs.append(managed)
def disable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for a scope."""
self._set_msg_status(
msgid, enable=_False, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line)
def disable_next(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Disable a message for the next line."""
if not line:
raise exceptions.NoLineSuppliedError
self._set_msg_status(
msgid,
enable=_False,
scope=scope,
line=line + 1,
ignore_unknown=ignore_unknown,
)
self._register_by_id_managed_msg(msgid, line + 1)
def enable(
self,
msgid: str,
scope: str = "package",
line: Optional[int] = None,
ignore_unknown: bool = _False,
) -> None:
"""Enable a message for a scope."""
self._set_msg_status(
msgid, enable=_True, scope=scope, line=line, ignore_unknown=ignore_unknown
)
self._register_by_id_managed_msg(msgid, line, is_disabled=_False)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.