Skip to content

Commit

Permalink
[performance] Check that 'trailing-comma-tuple' is enabled only once (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas authored May 13, 2024
1 parent b4a9535 commit 743a04d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/data/messages/t/trailing-comma-tuple/details.rst
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9608.performance
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")
Expand Down

0 comments on commit 743a04d

Please sign in to comment.