Skip to content

Commit

Permalink
Fix linterstats.get_module_message_count() (#9146) (#9648)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacob Walls <[email protected]>
(cherry picked from commit d7526e3)

Co-authored-by: zasca <[email protected]>
  • Loading branch information
github-actions[bot] and zasca authored May 18, 2024
1 parent aed496a commit 1498675
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9145.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Restore "errors / warnings by module" section to report output (with `-ry`).

Closes #9145
3 changes: 3 additions & 0 deletions pylint/lint/report_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions pylint/utils/linterstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1498675

Please sign in to comment.