aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2024-05-14 01:03:58 +0200
committerGitHub <noreply@github.com>2024-05-13 19:03:58 -0400
commit743a04de97e566968510bcd398ac93490beb252a (patch)
tree2d7272fdf3dbaa67f031b80463f5b53a6a588e1f
parentb4a9535566b149d5afe60b8e82d573441fbec0e6 (diff)
downloadpylint-upstream-main.tar.gz
[performance] Check that 'trailing-comma-tuple' is enabled only once (#9620)upstream-main
-rw-r--r--doc/data/messages/t/trailing-comma-tuple/details.rst2
-rw-r--r--doc/whatsnew/fragments/9608.performance4
-rw-r--r--pylint/checkers/refactoring/refactoring_checker.py10
3 files changed, 13 insertions, 3 deletions
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")