Skip to content

Commit 926547b

Browse files
[trailing-comma-tuple] Fix enabling with message control locally when disabled for the file (#9609) (#9649)
* [trailing-comma-tuple] Set the confidence to HIGH * [trailing-comma-tuple] Properly emit when disabled globally and enabled locally * [trailing-comma-tuple] Handle case with space between 'enable' and '=' (cherry picked from commit 96fad05) Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 1498675 commit 926547b

File tree

10 files changed

+96
-20
lines changed

10 files changed

+96
-20
lines changed

.pyenchant_pylint_custom_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ testoptions
336336
tmp
337337
tokencheckers
338338
tokeninfo
339+
tokenization
339340
tokenize
340341
tokenizer
341342
toml

doc/data/messages/t/trailing-comma-tuple/details.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

doc/whatsnew/fragments/9608.bugfix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``trailing-comma-tuple`` should now be correctly emitted when it was disabled globally
2+
but enabled via local message control, after removal of an over-optimisation.
3+
4+
Refs #9608.

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,29 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
652652
trailing_comma_tuple_enabled_for_file = self.linter.is_message_enabled(
653653
"trailing-comma-tuple"
654654
)
655+
trailing_comma_tuple_enabled_once: bool = trailing_comma_tuple_enabled_for_file
655656
# Process tokens and look for 'if' or 'elif'
656657
for index, token in enumerate(tokens):
657658
token_string = token[1]
659+
if (
660+
not trailing_comma_tuple_enabled_once
661+
and token_string.startswith("#")
662+
# We have at least 1 '#' (one char) at the start of the token
663+
and "pylint:" in token_string[1:]
664+
# We have at least '#' 'pylint' ( + ':') (8 chars) at the start of the token
665+
and "enable" in token_string[8:]
666+
# We have at least '#', 'pylint', ( + ':'), 'enable' (+ '=') (15 chars) at
667+
# the start of the token
668+
and any(
669+
c in token_string[15:] for c in ("trailing-comma-tuple", "R1707")
670+
)
671+
):
672+
# Way to not have to check if "trailing-comma-tuple" is enabled or
673+
# disabled on each line: Any enable for it during tokenization and
674+
# we'll start using the costly '_is_trailing_comma' to check if we
675+
# need to raise the message. We still won't raise if it's disabled
676+
# again due to the usual generic message control handling later.
677+
trailing_comma_tuple_enabled_once = True
658678
if token_string == "elif":
659679
# AST exists by the time process_tokens is called, so
660680
# it's safe to assume tokens[index+1] exists.
@@ -663,10 +683,17 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
663683
# token[2] is the actual position and also is
664684
# reported by IronPython.
665685
self._elifs.extend([token[2], tokens[index + 1][2]])
666-
elif trailing_comma_tuple_enabled_for_file and _is_trailing_comma(
667-
tokens, index
668-
):
669-
self.add_message("trailing-comma-tuple", line=token.start[0])
686+
elif (
687+
trailing_comma_tuple_enabled_for_file
688+
or trailing_comma_tuple_enabled_once
689+
) and _is_trailing_comma(tokens, index):
690+
# If "trailing-comma-tuple" is enabled globally we always check _is_trailing_comma
691+
# it might be for nothing if there's a local disable, or if the message control is
692+
# not enabling 'trailing-comma-tuple', but the alternative is having to check if
693+
# it's enabled for a line each line (just to avoid calling '_is_trailing_comma').
694+
self.add_message(
695+
"trailing-comma-tuple", line=token.start[0], confidence=HIGH
696+
)
670697

671698
@utils.only_required_for_messages("consider-using-with")
672699
def leave_module(self, _: nodes.Module) -> None:

pylint/lint/message_state_handler.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,14 @@ def is_message_enabled(
305305
line: int | None = None,
306306
confidence: interfaces.Confidence | None = None,
307307
) -> bool:
308-
"""Return whether this message is enabled for the current file, line and
309-
confidence level.
308+
"""Is this message enabled for the current file ?
310309
311-
This function can't be cached right now as the line is the line of
312-
the currently analysed file (self.file_state), if it changes, then the
313-
result for the same msg_descr/line might need to change.
310+
Optionally, is it enabled for this line and confidence level ?
311+
312+
The current file is implicit and mandatory. As a result this function
313+
can't be cached right now as the line is the line of the currently
314+
analysed file (self.file_state), if it changes, then the result for
315+
the same msg_descr/line might need to change.
314316
315317
:param msg_descr: Either the msgid or the symbol for a MessageDefinition
316318
:param line: The line of the currently analysed file

tests/functional/t/trailing_comma_tuple.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ def some_other_func():
4848

4949
JJJ = some_func(0,
5050
0)
51+
52+
# pylint: disable-next=trailing-comma-tuple
53+
AAA = 1,
54+
BBB = "aaaa", # [trailing-comma-tuple]
55+
# pylint: disable=trailing-comma-tuple
56+
CCC="aaa",
57+
III = some_func(0,
58+
0),
59+
# pylint: enable=trailing-comma-tuple
60+
FFF=['f'], # [trailing-comma-tuple]
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
trailing-comma-tuple:3:0:None:None::Disallow trailing comma tuple:UNDEFINED
2-
trailing-comma-tuple:4:0:None:None::Disallow trailing comma tuple:UNDEFINED
3-
trailing-comma-tuple:5:0:None:None::Disallow trailing comma tuple:UNDEFINED
4-
trailing-comma-tuple:6:0:None:None::Disallow trailing comma tuple:UNDEFINED
5-
trailing-comma-tuple:31:0:None:None::Disallow trailing comma tuple:UNDEFINED
6-
trailing-comma-tuple:34:0:None:None::Disallow trailing comma tuple:UNDEFINED
7-
trailing-comma-tuple:38:0:None:None::Disallow trailing comma tuple:UNDEFINED
8-
trailing-comma-tuple:41:0:None:None::Disallow trailing comma tuple:UNDEFINED
9-
trailing-comma-tuple:47:0:None:None::Disallow trailing comma tuple:UNDEFINED
1+
trailing-comma-tuple:3:0:None:None::Disallow trailing comma tuple:HIGH
2+
trailing-comma-tuple:4:0:None:None::Disallow trailing comma tuple:HIGH
3+
trailing-comma-tuple:5:0:None:None::Disallow trailing comma tuple:HIGH
4+
trailing-comma-tuple:6:0:None:None::Disallow trailing comma tuple:HIGH
5+
trailing-comma-tuple:31:0:None:None::Disallow trailing comma tuple:HIGH
6+
trailing-comma-tuple:34:0:None:None::Disallow trailing comma tuple:HIGH
7+
trailing-comma-tuple:38:0:None:None::Disallow trailing comma tuple:HIGH
8+
trailing-comma-tuple:41:0:None:None::Disallow trailing comma tuple:HIGH
9+
trailing-comma-tuple:47:0:None:None::Disallow trailing comma tuple:HIGH
10+
trailing-comma-tuple:54:0:None:None::Disallow trailing comma tuple:HIGH
11+
trailing-comma-tuple:60:0:None:None::Disallow trailing comma tuple:HIGH
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Check trailing comma tuple optimization."""
2+
# pylint: disable=missing-docstring
3+
4+
AAA = 1,
5+
BBB = "aaaa",
6+
CCC="aaa",
7+
FFF=['f'],
8+
9+
def some_func(first, second):
10+
if first:
11+
return first,
12+
if second:
13+
return (first, second,)
14+
return first, second,
15+
16+
#pylint:enable = trailing-comma-tuple
17+
AAA = 1, # [trailing-comma-tuple]
18+
BBB = "aaaa", # [trailing-comma-tuple]
19+
# pylint: disable=trailing-comma-tuple
20+
CCC="aaa",
21+
III = some_func(0,
22+
0),
23+
# pylint: enable=trailing-comma-tuple
24+
FFF=['f'], # [trailing-comma-tuple]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[MAIN]
2+
disable=trailing-comma-tuple
3+
4+
[testoptions]
5+
exclude_from_minimal_messages_config=True
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trailing-comma-tuple:17:0:None:None::Disallow trailing comma tuple:HIGH
2+
trailing-comma-tuple:18:0:None:None::Disallow trailing comma tuple:HIGH
3+
trailing-comma-tuple:24:0:None:None::Disallow trailing comma tuple:HIGH

0 commit comments

Comments
 (0)