@@ -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 :
0 commit comments