Skip to content

Commit

Permalink
[trailing-comma-tuple] Properly emit when disabled globally and enabl…
Browse files Browse the repository at this point in the history
…ed locally
  • Loading branch information
Pierre-Sassoulas committed May 18, 2024
1 parent 171e40f commit 2ea3405
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions .pyenchant_pylint_custom_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ testoptions
tmp
tokencheckers
tokeninfo
tokenization
tokenize
tokenizer
toml
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9608.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``trailing-comma-tuple`` should now be correctly emitted when it was disabled globally
but enabled via local message control, after removal of an over-optimisation.

Refs #9608.
24 changes: 21 additions & 3 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,22 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
trailing_comma_tuple_enabled_for_file = self.linter.is_message_enabled(
"trailing-comma-tuple"
)
trailing_comma_tuple_enabled_once: bool = trailing_comma_tuple_enabled_for_file
# Process tokens and look for 'if' or 'elif'
for index, token in enumerate(tokens):
token_string = token[1]
if (
not trailing_comma_tuple_enabled_once
and token_string.startswith("#")
and all(c in token_string for c in ("pylint:", "enable"))
and any(c in token_string for c in ("trailing-comma-tuple", "R1707"))
):
# Way to not have to check if "trailing-comma-tuple" is enabled or
# disabled on each line: Any enable for it during tokenization and
# we'll start using the costly '_is_trailing_comma' to check if we
# need to raise the message. We still won't raise if it's disabled
# again due to the usual generic message control handling later.
trailing_comma_tuple_enabled_once = True
if token_string == "elif":
# AST exists by the time process_tokens is called, so
# it's safe to assume tokens[index+1] exists.
Expand All @@ -663,9 +676,14 @@ 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 trailing_comma_tuple_enabled_for_file and _is_trailing_comma(
tokens, index
):
elif (
trailing_comma_tuple_enabled_for_file
or trailing_comma_tuple_enabled_once
) and _is_trailing_comma(tokens, index):
# If "trailing-comma-tuple" is enabled globally we always check _is_trailing_comma
# it might be for nothing if there's a local disable, or if the message control is
# not enabling 'trailing-comma-tuple', but the alternative is having to check if
# it's enabled for a line each line (just to avoid calling '_is_trailing_comma').
self.add_message(
"trailing-comma-tuple", line=token.start[0], confidence=HIGH
)
Expand Down
12 changes: 7 additions & 5 deletions pylint/lint/message_state_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,14 @@ def is_message_enabled(
line: int | None = None,
confidence: interfaces.Confidence | None = None,
) -> bool:
"""Return whether this message is enabled for the current file, line and
confidence level.
"""Is this message enabled for the current file ?
This function can't be cached right now as the line is the line of
the currently analysed file (self.file_state), if it changes, then the
result for the same msg_descr/line might need to change.
Optionally, is it enabled for this line and confidence level ?
The current file is implicit and mandatory. As a result this function
can't be cached right now as the line is the line of the currently
analysed file (self.file_state), if it changes, then the result for
the same msg_descr/line might need to change.
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
:param line: The line of the currently analysed file
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/t/trailing_comma_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ def some_other_func():

JJJ = some_func(0,
0)

# pylint: disable-next=trailing-comma-tuple
AAA = 1,
BBB = "aaaa", # [trailing-comma-tuple]
# pylint: disable=trailing-comma-tuple
CCC="aaa",
III = some_func(0,
0),
# pylint: enable=trailing-comma-tuple
FFF=['f'], # [trailing-comma-tuple]
2 changes: 2 additions & 0 deletions tests/functional/t/trailing_comma_tuple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ trailing-comma-tuple:34:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:38:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:41:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:47:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:54:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:60:0:None:None::Disallow trailing comma tuple:HIGH
24 changes: 24 additions & 0 deletions tests/functional/t/trailing_comma_tuple_9608.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Check trailing comma tuple optimization."""
# pylint: disable=missing-docstring

AAA = 1,
BBB = "aaaa",
CCC="aaa",
FFF=['f'],

def some_func(first, second):
if first:
return first,
if second:
return (first, second,)
return first, second,

# pylint: enable=trailing-comma-tuple
AAA = 1, # [trailing-comma-tuple]
BBB = "aaaa", # [trailing-comma-tuple]
# pylint: disable=trailing-comma-tuple
CCC="aaa",
III = some_func(0,
0),
# pylint: enable=trailing-comma-tuple
FFF=['f'], # [trailing-comma-tuple]
5 changes: 5 additions & 0 deletions tests/functional/t/trailing_comma_tuple_9608.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[MAIN]
disable=trailing-comma-tuple

[testoptions]
exclude_from_minimal_messages_config=True
3 changes: 3 additions & 0 deletions tests/functional/t/trailing_comma_tuple_9608.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trailing-comma-tuple:17:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:18:0:None:None::Disallow trailing comma tuple:HIGH
trailing-comma-tuple:24:0:None:None::Disallow trailing comma tuple:HIGH

0 comments on commit 2ea3405

Please sign in to comment.