From afcbe6db58c0925d8c4ef77f142077aed68e7083 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 7 Nov 2021 15:13:05 +0100 Subject: [PATCH 1/5] Create and use a function for module stats initialization This permit to reduce the coupling between Pylinter and linterstats. Refactor prior to #4720 --- pylint/lint/pylinter.py | 14 ++------------ pylint/utils/linterstats.py | 10 +++++++--- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 4c97566963..7f40050703 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -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, @@ -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): @@ -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: diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index f460544558..18a5b50520 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -149,6 +149,12 @@ def __str__(self) -> str: {self.nb_duplicated_lines} {self.percent_duplicated_lines}""" + def init_single_module(self, module_name: str) -> None: + """Initialize module statistics if required.""" + 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[ @@ -285,9 +291,7 @@ def increase_single_module_message_count( 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""" From f112b399a83073a7f1335efd2f98bf54594e1d77 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 7 Nov 2021 17:08:34 +0100 Subject: [PATCH 2/5] Update pylint/utils/linterstats.py --- pylint/utils/linterstats.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index 18a5b50520..b4d41dd4ea 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -150,7 +150,6 @@ def __str__(self) -> str: {self.percent_duplicated_lines}""" def init_single_module(self, module_name: str) -> None: - """Initialize module statistics if required.""" self.by_module[module_name] = ModuleStats( convention=0, error=0, fatal=0, info=0, refactor=0, statement=0, warning=0 ) From 12f2842fff3beefec621212247ae87523b0d7103 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 7 Nov 2021 22:37:55 +0100 Subject: [PATCH 3/5] Add two missing litteral in typing for module stats --- pylint/utils/linterstats.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index b4d41dd4ea..5729ca40ca 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -286,7 +286,9 @@ def increase_single_message_count(self, type_name: str, increase: int) -> None: def increase_single_module_message_count( self, modname: str, - type_name: Literal["convention", "error", "fatal", "info", "refactor"], + type_name: Literal[ + "convention", "error", "fatal", "info", "refactor", "statement", "warning" + ], increase: int, ) -> None: """Increase the message type count of an individual message type of a module""" From 47b9a4e9e9ca2e55c13dd56c25e33c8bad532e1d Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 7 Nov 2021 23:19:02 +0100 Subject: [PATCH 4/5] Add independant typing for ModuleStats attribute --- pylint/utils/linterstats.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index 5729ca40ca..a9b3708c9d 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -73,6 +73,10 @@ 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""" @@ -284,12 +288,7 @@ 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", "statement", "warning" - ], - 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] += increase From 4702343e09d5a6708a7cdcd67c5cb5c32bc0c662 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 7 Nov 2021 23:28:48 +0100 Subject: [PATCH 5/5] Add a comment following review See https://github.com/PyCQA/pylint/pull/5271\#issuecomment-962637958 --- pylint/utils/linterstats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index a9b3708c9d..cb20853522 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -77,6 +77,7 @@ class ModuleStats(TypedDict): "convention", "error", "fatal", "info", "refactor", "statement", "warning" ] + # pylint: disable-next=too-many-instance-attributes class LinterStats: """Class used to linter stats""" @@ -154,6 +155,7 @@ def __str__(self) -> str: {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 )