Skip to content

Commit daeab1f

Browse files
[trailing-comma-tuple] Properly emit when disabled globally and enabled locally
1 parent 171e40f commit daeab1f

File tree

10 files changed

+81
-10
lines changed

10 files changed

+81
-10
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: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,26 @@ 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 all(c in token_string[1:] for c in ("pylint:", "enable="))
664+
# We have at least '#' 'pylint' / 'enable' (15 char) at the
665+
and any( # start of the token
666+
c in token_string[15:] for c in ("trailing-comma-tuple", "R1707")
667+
)
668+
):
669+
# Way to not have to check if "trailing-comma-tuple" is enabled or
670+
# disabled on each line: Any enable for it during tokenization and
671+
# we'll start using the costly '_is_trailing_comma' to check if we
672+
# need to raise the message. We still won't raise if it's disabled
673+
# again due to the usual generic message control handling later.
674+
trailing_comma_tuple_enabled_once = True
658675
if token_string == "elif":
659676
# AST exists by the time process_tokens is called, so
660677
# it's safe to assume tokens[index+1] exists.
@@ -663,9 +680,14 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
663680
# token[2] is the actual position and also is
664681
# reported by IronPython.
665682
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-
):
683+
elif (
684+
trailing_comma_tuple_enabled_for_file
685+
or trailing_comma_tuple_enabled_once
686+
) and _is_trailing_comma(tokens, index):
687+
# If "trailing-comma-tuple" is enabled globally we always check _is_trailing_comma
688+
# it might be for nothing if there's a local disable, or if the message control is
689+
# not enabling 'trailing-comma-tuple', but the alternative is having to check if
690+
# it's enabled for a line each line (just to avoid calling '_is_trailing_comma').
669691
self.add_message(
670692
"trailing-comma-tuple", line=token.start[0], confidence=HIGH
671693
)

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]

tests/functional/t/trailing_comma_tuple.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ trailing-comma-tuple:34:0:None:None::Disallow trailing comma tuple:HIGH
77
trailing-comma-tuple:38:0:None:None::Disallow trailing comma tuple:HIGH
88
trailing-comma-tuple:41:0:None:None::Disallow trailing comma tuple:HIGH
99
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)