Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linterstats.get_module_message_count() #9146

Merged
merged 4 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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