diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-09-15 20:42:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-15 20:42:22 +0200 |
commit | 22e56c07cf745d695df1d52fe3988cc071f0951b (patch) | |
tree | 713b888a2f24239932b7d8a6a1d9f8bffe0026cd /pylint/lint/report_functions.py | |
parent | cb896128b0e8f62c0650e980ef77a3c8af21ef8d (diff) | |
download | pylint-git-22e56c07cf745d695df1d52fe3988cc071f0951b.tar.gz |
Add typing to all calls to ``self.stats`` (#4973)
* Add typing to all calls to ``self.stats``
All checkers inherit from a baseclass which has a ``stats`` attribute.
This attribute has a fairly unmanageable type, but the current typing includes all variations of the attribute.
Other changes not directly related to ``self.stats`` are due to ``mypy``warnings.
This incorporate the feedback received in #4954
* Add ``CheckerStatistic`` class to ``pylint/typing``
* Guard `typing.Counter` import
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Diffstat (limited to 'pylint/lint/report_functions.py')
-rw-r--r-- | pylint/lint/report_functions.py | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/pylint/lint/report_functions.py b/pylint/lint/report_functions.py index 21cb3b824..12c4b0335 100644 --- a/pylint/lint/report_functions.py +++ b/pylint/lint/report_functions.py @@ -2,12 +2,18 @@ # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE import collections +from typing import DefaultDict, Dict, List, Tuple, Union from pylint import checkers, exceptions from pylint.reporters.ureports import nodes as report_nodes +from pylint.typing import CheckerStats -def report_total_messages_stats(sect, stats, previous_stats): +def report_total_messages_stats( + sect, + stats: CheckerStats, + previous_stats: CheckerStats, +): """make total errors / warnings report""" lines = ["type", "number", "previous", "difference"] lines += checkers.table_lines_from_stats( @@ -16,14 +22,19 @@ def report_total_messages_stats(sect, stats, previous_stats): sect.append(report_nodes.Table(children=lines, cols=4, rheaders=1)) -def report_messages_stats(sect, stats, _): +def report_messages_stats( + sect, + stats: CheckerStats, + _: CheckerStats, +): """make messages type report""" if not stats["by_msg"]: # don't print this report when we didn't detected any errors raise exceptions.EmptyReportError() - in_order = sorted( + by_msg_stats: Dict[str, int] = stats["by_msg"] # type: ignore + in_order: List[Tuple[int, str]] = sorted( (value, msg_id) - for msg_id, value in stats["by_msg"].items() + for msg_id, value in by_msg_stats.items() if not msg_id.startswith("I") ) in_order.reverse() @@ -33,16 +44,23 @@ def report_messages_stats(sect, stats, _): sect.append(report_nodes.Table(children=lines, cols=2, rheaders=1)) -def report_messages_by_module_stats(sect, stats, _): +def report_messages_by_module_stats( + sect, + stats: CheckerStats, + _: CheckerStats, +): """make errors / warnings by modules report""" - if len(stats["by_module"]) == 1: + module_stats: Dict[str, Dict[str, int]] = stats["by_module"] # type: ignore + if len(module_stats) == 1: # don't print this report when we are analysing a single module raise exceptions.EmptyReportError() - by_mod = collections.defaultdict(dict) + by_mod: DefaultDict[str, Dict[str, Union[int, float]]] = collections.defaultdict( + dict + ) for m_type in ("fatal", "error", "warning", "refactor", "convention"): - total = stats[m_type] - for module in stats["by_module"].keys(): - mod_total = stats["by_module"][module][m_type] + total: int = stats[m_type] # type: ignore + for module in module_stats.keys(): + mod_total = module_stats[module][m_type] percent = 0 if total == 0 else float((mod_total) * 100) / total by_mod[module][m_type] = percent sorted_result = [] |