Skip to content

[performance] Check that 'trailing-comma-tuple' is enabled only once #9620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading