Skip to content

Commit c6fcd01

Browse files
Create and use a function for module stats initialization (#5271)
This permit to reduce the coupling between Pylinter and linterstats. Also add two missing litteral in typing for module stats and independant typing for ModuleStats attribute Refactor prior to #4720
1 parent 6827dfe commit c6fcd01

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

pylint/lint/pylinter.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@
4646
MessageLocationTuple,
4747
ModuleDescriptionDict,
4848
)
49-
from pylint.utils import (
50-
ASTWalker,
51-
FileState,
52-
LinterStats,
53-
ModuleStats,
54-
get_global_option,
55-
utils,
56-
)
49+
from pylint.utils import ASTWalker, FileState, LinterStats, get_global_option, utils
5750
from pylint.utils.pragma_parser import (
5851
OPTION_PO,
5952
InvalidPragmaError,
@@ -1125,9 +1118,7 @@ def set_current_module(self, modname, filepath: Optional[str] = None):
11251118
self.reporter.on_set_current_module(modname, filepath)
11261119
self.current_name = modname
11271120
self.current_file = filepath or modname
1128-
self.stats.by_module[modname] = ModuleStats(
1129-
convention=0, error=0, fatal=0, info=0, refactor=0, statement=0, warning=0
1130-
)
1121+
self.stats.init_single_module(modname)
11311122

11321123
@contextlib.contextmanager
11331124
def _astroid_module_checker(self):
@@ -1435,7 +1426,6 @@ def _add_one_message(
14351426
# update stats
14361427
msg_cat = MSG_TYPES[message_definition.msgid[0]]
14371428
self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]]
1438-
14391429
self.stats.increase_single_message_count(msg_cat, 1)
14401430
self.stats.increase_single_module_message_count(self.current_name, msg_cat, 1)
14411431
try:

pylint/utils/linterstats.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class ModuleStats(TypedDict):
7373
warning: int
7474

7575

76+
ModuleStatsAttribute = Literal[
77+
"convention", "error", "fatal", "info", "refactor", "statement", "warning"
78+
]
79+
80+
7681
# pylint: disable-next=too-many-instance-attributes
7782
class LinterStats:
7883
"""Class used to linter stats"""
@@ -149,6 +154,12 @@ def __str__(self) -> str:
149154
{self.nb_duplicated_lines}
150155
{self.percent_duplicated_lines}"""
151156

157+
def init_single_module(self, module_name: str) -> None:
158+
"""Use through Pylinter.set_current_module so Pyliner.current_name is consistent."""
159+
self.by_module[module_name] = ModuleStats(
160+
convention=0, error=0, fatal=0, info=0, refactor=0, statement=0, warning=0
161+
)
162+
152163
def get_bad_names(
153164
self,
154165
node_name: Literal[
@@ -279,15 +290,10 @@ def increase_single_message_count(self, type_name: str, increase: int) -> None:
279290
setattr(self, type_name, getattr(self, type_name) + increase)
280291

281292
def increase_single_module_message_count(
282-
self,
283-
modname: str,
284-
type_name: Literal["convention", "error", "fatal", "info", "refactor"],
285-
increase: int,
293+
self, modname: str, type_name: ModuleStatsAttribute, increase: int
286294
) -> None:
287295
"""Increase the message type count of an individual message type of a module"""
288-
self.by_module[modname][type_name] = (
289-
self.by_module[modname][type_name] + increase
290-
)
296+
self.by_module[modname][type_name] += increase
291297

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

0 commit comments

Comments
 (0)