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
Performance analysis by correctmost

Refs pylint-dev#8606 (follow-up)
Closes pylint-dev#9608
  • Loading branch information
Pierre-Sassoulas committed May 13, 2024
1 parent 4f0c7ac commit 14af968
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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 instead of once for each token.

Refs #9608.
14 changes: 11 additions & 3 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ def _init(self) -> None:
self._reported_swap_nodes: set[nodes.NodeNG] = set()
self._can_simplify_bool_op: bool = False
self._consider_using_with_stack.clear_all()
self.trailing_comma_tuple_enabled_globally = False

def open(self) -> None:
# do this in open since config not fully initialized in __init__
Expand All @@ -550,6 +551,9 @@ def open(self) -> None:
self._suggest_join_with_non_empty_separator = (
self.linter.config.suggest_join_with_non_empty_separator
)
self.trailing_comma_tuple_enabled_globally = self.linter.is_message_enabled(
"trailing-comma-tuple"
)

@cached_property
def _dummy_rgx(self) -> Pattern[str]:
Expand Down Expand Up @@ -648,6 +652,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 +667,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 14af968

Please sign in to comment.