From 926547bbbfcedd77886ec1f73ae1463daac2fa7f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 18 May 2024 15:49:41 +0000 Subject: [PATCH] [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 96fad051496645a0632f8fdd97e744bf5309946b) Co-authored-by: Pierre Sassoulas --- .pyenchant_pylint_custom_dict.txt | 1 + .../t/trailing-comma-tuple/details.rst | 2 -- doc/whatsnew/fragments/9608.bugfix | 4 +++ .../refactoring/refactoring_checker.py | 35 ++++++++++++++++--- pylint/lint/message_state_handler.py | 12 ++++--- tests/functional/t/trailing_comma_tuple.py | 10 ++++++ tests/functional/t/trailing_comma_tuple.txt | 20 ++++++----- .../functional/t/trailing_comma_tuple_9608.py | 24 +++++++++++++ .../functional/t/trailing_comma_tuple_9608.rc | 5 +++ .../t/trailing_comma_tuple_9608.txt | 3 ++ 10 files changed, 96 insertions(+), 20 deletions(-) delete mode 100644 doc/data/messages/t/trailing-comma-tuple/details.rst create mode 100644 doc/whatsnew/fragments/9608.bugfix create mode 100644 tests/functional/t/trailing_comma_tuple_9608.py create mode 100644 tests/functional/t/trailing_comma_tuple_9608.rc create mode 100644 tests/functional/t/trailing_comma_tuple_9608.txt diff --git a/.pyenchant_pylint_custom_dict.txt b/.pyenchant_pylint_custom_dict.txt index 4bcff9c931..78d861aea3 100644 --- a/.pyenchant_pylint_custom_dict.txt +++ b/.pyenchant_pylint_custom_dict.txt @@ -336,6 +336,7 @@ testoptions tmp tokencheckers tokeninfo +tokenization tokenize tokenizer toml diff --git a/doc/data/messages/t/trailing-comma-tuple/details.rst b/doc/data/messages/t/trailing-comma-tuple/details.rst deleted file mode 100644 index 272190df69..0000000000 --- a/doc/data/messages/t/trailing-comma-tuple/details.rst +++ /dev/null @@ -1,2 +0,0 @@ -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. diff --git a/doc/whatsnew/fragments/9608.bugfix b/doc/whatsnew/fragments/9608.bugfix new file mode 100644 index 0000000000..badcf32d19 --- /dev/null +++ b/doc/whatsnew/fragments/9608.bugfix @@ -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. diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 999b95edac..94e722b177 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -652,9 +652,29 @@ 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("#") + # We have at least 1 '#' (one char) at the start of the token + and "pylint:" in token_string[1:] + # We have at least '#' 'pylint' ( + ':') (8 chars) at the start of the token + and "enable" in token_string[8:] + # We have at least '#', 'pylint', ( + ':'), 'enable' (+ '=') (15 chars) at + # the start of the token + and any( + c in token_string[15:] 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. @@ -663,10 +683,17 @@ 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 - ): - self.add_message("trailing-comma-tuple", line=token.start[0]) + 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 + ) @utils.only_required_for_messages("consider-using-with") def leave_module(self, _: nodes.Module) -> None: diff --git a/pylint/lint/message_state_handler.py b/pylint/lint/message_state_handler.py index 26028f0fab..2ddd7d4db3 100644 --- a/pylint/lint/message_state_handler.py +++ b/pylint/lint/message_state_handler.py @@ -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 diff --git a/tests/functional/t/trailing_comma_tuple.py b/tests/functional/t/trailing_comma_tuple.py index de60184cab..8effe475ec 100644 --- a/tests/functional/t/trailing_comma_tuple.py +++ b/tests/functional/t/trailing_comma_tuple.py @@ -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] diff --git a/tests/functional/t/trailing_comma_tuple.txt b/tests/functional/t/trailing_comma_tuple.txt index 9984e5afb7..d65ad72ed8 100644 --- a/tests/functional/t/trailing_comma_tuple.txt +++ b/tests/functional/t/trailing_comma_tuple.txt @@ -1,9 +1,11 @@ -trailing-comma-tuple:3:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:4:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:5:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:6:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:31:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:34:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:38:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:41:0:None:None::Disallow trailing comma tuple:UNDEFINED -trailing-comma-tuple:47:0:None:None::Disallow trailing comma tuple:UNDEFINED +trailing-comma-tuple:3:0:None:None::Disallow trailing comma tuple:HIGH +trailing-comma-tuple:4:0:None:None::Disallow trailing comma tuple:HIGH +trailing-comma-tuple:5:0:None:None::Disallow trailing comma tuple:HIGH +trailing-comma-tuple:6:0:None:None::Disallow trailing comma tuple:HIGH +trailing-comma-tuple:31:0:None:None::Disallow trailing comma tuple:HIGH +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 diff --git a/tests/functional/t/trailing_comma_tuple_9608.py b/tests/functional/t/trailing_comma_tuple_9608.py new file mode 100644 index 0000000000..6ca408c2b9 --- /dev/null +++ b/tests/functional/t/trailing_comma_tuple_9608.py @@ -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] diff --git a/tests/functional/t/trailing_comma_tuple_9608.rc b/tests/functional/t/trailing_comma_tuple_9608.rc new file mode 100644 index 0000000000..80157090eb --- /dev/null +++ b/tests/functional/t/trailing_comma_tuple_9608.rc @@ -0,0 +1,5 @@ +[MAIN] +disable=trailing-comma-tuple + +[testoptions] +exclude_from_minimal_messages_config=True diff --git a/tests/functional/t/trailing_comma_tuple_9608.txt b/tests/functional/t/trailing_comma_tuple_9608.txt new file mode 100644 index 0000000000..b6ea91784b --- /dev/null +++ b/tests/functional/t/trailing_comma_tuple_9608.txt @@ -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