Skip to content

Create and use a function for module stats initialization #5271

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

Merged
merged 5 commits into from
Nov 8, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,7 @@
MessageLocationTuple,
ModuleDescriptionDict,
)
from pylint.utils import (
ASTWalker,
FileState,
LinterStats,
ModuleStats,
get_global_option,
utils,
)
from pylint.utils import ASTWalker, FileState, LinterStats, get_global_option, utils
from pylint.utils.pragma_parser import (
OPTION_PO,
InvalidPragmaError,
Expand Down Expand Up @@ -1125,9 +1118,7 @@ def set_current_module(self, modname, filepath: Optional[str] = None):
self.reporter.on_set_current_module(modname, filepath)
self.current_name = modname
self.current_file = filepath or modname
self.stats.by_module[modname] = ModuleStats(
convention=0, error=0, fatal=0, info=0, refactor=0, statement=0, warning=0
)
self.stats.init_single_module(modname)

@contextlib.contextmanager
def _astroid_module_checker(self):
Expand Down Expand Up @@ -1435,7 +1426,6 @@ def _add_one_message(
# 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, msg_cat, 1)
try:
Expand Down
20 changes: 13 additions & 7 deletions pylint/utils/linterstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class ModuleStats(TypedDict):
warning: int


ModuleStatsAttribute = Literal[
"convention", "error", "fatal", "info", "refactor", "statement", "warning"
]


# pylint: disable-next=too-many-instance-attributes
class LinterStats:
"""Class used to linter stats"""
Expand Down Expand Up @@ -149,6 +154,12 @@ def __str__(self) -> str:
{self.nb_duplicated_lines}
{self.percent_duplicated_lines}"""

def init_single_module(self, module_name: str) -> None:
"""Use through Pylinter.set_current_module so Pyliner.current_name is consistent."""
self.by_module[module_name] = ModuleStats(
convention=0, error=0, fatal=0, info=0, refactor=0, statement=0, warning=0
)

def get_bad_names(
self,
node_name: Literal[
Expand Down Expand Up @@ -279,15 +290,10 @@ def increase_single_message_count(self, type_name: str, increase: int) -> None:
setattr(self, type_name, getattr(self, type_name) + increase)

def increase_single_module_message_count(
self,
modname: str,
type_name: Literal["convention", "error", "fatal", "info", "refactor"],
increase: int,
self, modname: str, type_name: ModuleStatsAttribute, increase: int
) -> None:
"""Increase the message type count of an individual message type of a module"""
self.by_module[modname][type_name] = (
self.by_module[modname][type_name] + increase
)
self.by_module[modname][type_name] += increase

def reset_message_count(self) -> None:
"""Resets the message type count of the stats object"""
Expand Down