diff --git a/doc/whatsnew/fragments/9145.bugfix b/doc/whatsnew/fragments/9145.bugfix new file mode 100644 index 0000000000..9cb32d9c67 --- /dev/null +++ b/doc/whatsnew/fragments/9145.bugfix @@ -0,0 +1,3 @@ +Restore "errors / warnings by module" section to report output (with `-ry`). + +Closes #9145 diff --git a/pylint/lint/report_functions.py b/pylint/lint/report_functions.py index da7ab5fbc6..72734e4688 100644 --- a/pylint/lint/report_functions.py +++ b/pylint/lint/report_functions.py @@ -6,9 +6,11 @@ import collections from collections import defaultdict +from typing import cast from pylint import checkers, exceptions from pylint.reporters.ureports.nodes import Section, Table +from pylint.typing import MessageTypesFullName from pylint.utils import LinterStats @@ -54,6 +56,7 @@ def report_messages_by_module_stats( raise exceptions.EmptyReportError() by_mod: defaultdict[str, dict[str, int | float]] = collections.defaultdict(dict) for m_type in ("fatal", "error", "warning", "refactor", "convention"): + m_type = cast(MessageTypesFullName, m_type) total = stats.get_global_message_count(m_type) for module in module_stats.keys(): mod_total = stats.get_module_message_count(module, m_type) diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index e7a088b7bb..53afbcfe21 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -292,9 +292,11 @@ def get_global_message_count(self, type_name: str) -> int: """Get a global message count.""" return getattr(self, type_name, 0) - def get_module_message_count(self, modname: str, type_name: str) -> int: + def get_module_message_count( + self, modname: str, type_name: MessageTypesFullName + ) -> int: """Get a module message count.""" - return getattr(self.by_module[modname], type_name, 0) + return self.by_module[modname].get(type_name, 0) def increase_single_message_count(self, type_name: str, increase: int) -> None: """Increase the message type count of an individual message type.""" diff --git a/tests/test_self.py b/tests/test_self.py index 851191e2ae..1c72bc95f8 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -988,6 +988,11 @@ def test_can_list_directories_without_dunder_init(tmp_path: Path) -> None: stderr=subprocess.PIPE, ) + def test_warnings_by_module(self) -> None: + path = join(HERE, "regrtest_data", "unused_variable.py") + expected = "errors / warnings by module" + self._test_output([path, "-ry"], expected_output=expected) + @pytest.mark.needs_two_cores def test_jobs_score(self) -> None: path = join(HERE, "regrtest_data", "unused_variable.py")