From 754874456e4bc88f0ead8fab860245abf5c568ca Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 7 May 2024 22:06:54 +0200 Subject: [PATCH] [performance] Check that 'trailing-comma-tuple' is enabled only once Performance analysis by correctmost. Follow up to fix the known limitation will be in #9609 Refs #8606 (follow-up) Closes #9608 --- 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 0000000000..272190df69 --- /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 0000000000..3d34f20b45 --- /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 24d13c3a96..999b95edac 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -648,6 +648,10 @@ def _check_simplifiable_if(self, node: nodes.If) -> None: 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 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None: # 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")