From 743a04de97e566968510bcd398ac93490beb252a Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 14 May 2024 01:03:58 +0200 Subject: [performance] Check that 'trailing-comma-tuple' is enabled only once (#9620) --- doc/data/messages/t/trailing-comma-tuple/details.rst | 2 ++ doc/whatsnew/fragments/9608.performance | 4 ++++ pylint/checkers/refactoring/refactoring_checker.py | 10 +++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 doc/data/messages/t/trailing-comma-tuple/details.rst create mode 100644 doc/whatsnew/fragments/9608.performance diff --git a/doc/data/messages/t/trailing-comma-tuple/details.rst b/doc/data/messages/t/trailing-comma-tuple/details.rst new file mode 100644 index 000000000..272190df6 --- /dev/null +++ b/doc/data/messages/t/trailing-comma-tuple/details.rst @@ -0,0 +1,2 @@ +Known issue: It's impossible to reactivate ``trailing-comma-tuple`` using message control +once it's been disabled for a file due to over-optimization. diff --git a/doc/whatsnew/fragments/9608.performance b/doc/whatsnew/fragments/9608.performance new file mode 100644 index 000000000..3d34f20b4 --- /dev/null +++ b/doc/whatsnew/fragments/9608.performance @@ -0,0 +1,4 @@ +An internal check for ``trailing-comma-tuple`` being globally enabled or not is now +done once per file instead of once for each token. + +Refs #9608. diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 24d13c3a9..999b95eda 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -648,6 +648,10 @@ class RefactoringChecker(checkers.BaseTokenChecker): self.add_message("simplifiable-if-statement", node=node, args=(reduced_to,)) def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None: + # Optimization flag because '_is_trailing_comma' is costly + trailing_comma_tuple_enabled_for_file = self.linter.is_message_enabled( + "trailing-comma-tuple" + ) # Process tokens and look for 'if' or 'elif' for index, token in enumerate(tokens): token_string = token[1] @@ -659,9 +663,9 @@ class RefactoringChecker(checkers.BaseTokenChecker): # token[2] is the actual position and also is # reported by IronPython. self._elifs.extend([token[2], tokens[index + 1][2]]) - elif self.linter.is_message_enabled( - "trailing-comma-tuple" - ) and _is_trailing_comma(tokens, index): + elif trailing_comma_tuple_enabled_for_file and _is_trailing_comma( + tokens, index + ): self.add_message("trailing-comma-tuple", line=token.start[0]) @utils.only_required_for_messages("consider-using-with") -- cgit v1.2.3