From 40a229ab8a9025ae6ac10c085f562b6ae70a24b3 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 5 Jun 2024 22:30:14 +0200 Subject: [PATCH 1/3] [multiple-statements] Add more test cases to cover pass / ... --- tests/functional/m/multiple_statements.py | 15 +++++++++++++++ tests/functional/m/multiple_statements.txt | 16 +++++++++++----- .../m/multiple_statements_single_line.py | 15 ++++++++++++++- .../m/multiple_statements_single_line.txt | 6 ++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/tests/functional/m/multiple_statements.py b/tests/functional/m/multiple_statements.py index c3252f797c..6b90403d5e 100644 --- a/tests/functional/m/multiple_statements.py +++ b/tests/functional/m/multiple_statements.py @@ -4,13 +4,28 @@ from typing import overload +if True: print("Golfing sure is nice") # [multiple-statements] if True: pass # [multiple-statements] +if True: ... # [multiple-statements] + +if True: print("Golfing sure is nice") # [multiple-statements] +else: + pass if True: pass # [multiple-statements] else: pass +if True: ... # [multiple-statements] +else: + pass + +# The following difference in behavior is due to black 2024's style +# that reformat pass on multiple line but reformat "..." on a single line +# (only for classes, not for the examples above) +class MyException(Exception): print("Golfing sure is nice") # [multiple-statements] class MyError(Exception): pass # [multiple-statements] +class DebugTrueDetected(Exception): ... # [multiple-statements] class MyError(Exception): a='a' # [multiple-statements] diff --git a/tests/functional/m/multiple_statements.txt b/tests/functional/m/multiple_statements.txt index 661314268d..c76aa7a845 100644 --- a/tests/functional/m/multiple_statements.txt +++ b/tests/functional/m/multiple_statements.txt @@ -1,5 +1,11 @@ -multiple-statements:7:9:7:13::More than one statement on a single line:UNDEFINED -multiple-statements:9:9:9:13::More than one statement on a single line:UNDEFINED -multiple-statements:13:26:13:30:MyError:More than one statement on a single line:UNDEFINED -multiple-statements:15:26:15:31:MyError:More than one statement on a single line:UNDEFINED -multiple-statements:17:26:17:31:MyError:More than one statement on a single line:UNDEFINED +multiple-statements:7:9:7:38::More than one statement on a single line:UNDEFINED +multiple-statements:8:9:8:13::More than one statement on a single line:UNDEFINED +multiple-statements:9:9:9:12::More than one statement on a single line:UNDEFINED +multiple-statements:11:9:11:38::More than one statement on a single line:UNDEFINED +multiple-statements:15:9:15:13::More than one statement on a single line:UNDEFINED +multiple-statements:19:9:19:12::More than one statement on a single line:UNDEFINED +multiple-statements:26:30:26:59:MyException:More than one statement on a single line:UNDEFINED +multiple-statements:27:26:27:30:MyError:More than one statement on a single line:UNDEFINED +multiple-statements:28:36:28:39:DebugTrueDetected:More than one statement on a single line:UNDEFINED +multiple-statements:30:26:30:31:MyError:More than one statement on a single line:UNDEFINED +multiple-statements:32:26:32:31:MyError:More than one statement on a single line:UNDEFINED diff --git a/tests/functional/m/multiple_statements_single_line.py b/tests/functional/m/multiple_statements_single_line.py index 93a470702c..8b39d631bd 100644 --- a/tests/functional/m/multiple_statements_single_line.py +++ b/tests/functional/m/multiple_statements_single_line.py @@ -4,19 +4,32 @@ from typing import overload +if True: print("Golfing sure is nice") if True: pass +if True: ... + +if True: print("Golfing sure is nice") # [multiple-statements] +else: + pass if True: pass # [multiple-statements] else: pass +if True: ... # [multiple-statements] +else: + pass + +class MyException(Exception): print("Golfing sure is nice") class MyError(Exception): pass +class DebugTrueDetected(Exception): ... + class MyError(Exception): a='a' class MyError(Exception): a='a'; b='b' # [multiple-statements] -try: +try: #@ pass except: pass diff --git a/tests/functional/m/multiple_statements_single_line.txt b/tests/functional/m/multiple_statements_single_line.txt index cac2f7eb2e..52b2417768 100644 --- a/tests/functional/m/multiple_statements_single_line.txt +++ b/tests/functional/m/multiple_statements_single_line.txt @@ -1,2 +1,4 @@ -multiple-statements:9:9:9:13::More than one statement on a single line:UNDEFINED -multiple-statements:17:26:17:31:MyError:More than one statement on a single line:UNDEFINED +multiple-statements:11:9:11:38::More than one statement on a single line:UNDEFINED +multiple-statements:15:9:15:13::More than one statement on a single line:UNDEFINED +multiple-statements:19:9:19:12::More than one statement on a single line:UNDEFINED +multiple-statements:30:26:30:31:MyError:More than one statement on a single line:UNDEFINED From a1951c7ce763a73e35ac6c8e7273e69cd5140480 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 5 Jun 2024 22:50:00 +0200 Subject: [PATCH 2/3] [multiple-statements] Define the confidence as HIGH --- pylint/checkers/format.py | 2 +- tests/functional/m/multiple_statements.txt | 22 +++++++++---------- .../m/multiple_statements_single_line.txt | 8 +++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py index f4de616598..1e942e3190 100644 --- a/pylint/checkers/format.py +++ b/pylint/checkers/format.py @@ -548,7 +548,7 @@ def _check_multi_statement_line(self, node: nodes.NodeNG, line: int) -> None: ): return - self.add_message("multiple-statements", node=node) + self.add_message("multiple-statements", node=node, confidence=HIGH) self._visited_lines[line] = 2 def check_trailing_whitespace_ending(self, line: str, i: int) -> None: diff --git a/tests/functional/m/multiple_statements.txt b/tests/functional/m/multiple_statements.txt index c76aa7a845..3bf4719ea2 100644 --- a/tests/functional/m/multiple_statements.txt +++ b/tests/functional/m/multiple_statements.txt @@ -1,11 +1,11 @@ -multiple-statements:7:9:7:38::More than one statement on a single line:UNDEFINED -multiple-statements:8:9:8:13::More than one statement on a single line:UNDEFINED -multiple-statements:9:9:9:12::More than one statement on a single line:UNDEFINED -multiple-statements:11:9:11:38::More than one statement on a single line:UNDEFINED -multiple-statements:15:9:15:13::More than one statement on a single line:UNDEFINED -multiple-statements:19:9:19:12::More than one statement on a single line:UNDEFINED -multiple-statements:26:30:26:59:MyException:More than one statement on a single line:UNDEFINED -multiple-statements:27:26:27:30:MyError:More than one statement on a single line:UNDEFINED -multiple-statements:28:36:28:39:DebugTrueDetected:More than one statement on a single line:UNDEFINED -multiple-statements:30:26:30:31:MyError:More than one statement on a single line:UNDEFINED -multiple-statements:32:26:32:31:MyError:More than one statement on a single line:UNDEFINED +multiple-statements:7:9:7:38::More than one statement on a single line:HIGH +multiple-statements:8:9:8:13::More than one statement on a single line:HIGH +multiple-statements:9:9:9:12::More than one statement on a single line:HIGH +multiple-statements:11:9:11:38::More than one statement on a single line:HIGH +multiple-statements:15:9:15:13::More than one statement on a single line:HIGH +multiple-statements:19:9:19:12::More than one statement on a single line:HIGH +multiple-statements:26:30:26:59:MyException:More than one statement on a single line:HIGH +multiple-statements:27:26:27:30:MyError:More than one statement on a single line:HIGH +multiple-statements:28:36:28:39:DebugTrueDetected:More than one statement on a single line:HIGH +multiple-statements:30:26:30:31:MyError:More than one statement on a single line:HIGH +multiple-statements:32:26:32:31:MyError:More than one statement on a single line:HIGH diff --git a/tests/functional/m/multiple_statements_single_line.txt b/tests/functional/m/multiple_statements_single_line.txt index 52b2417768..8d19c72516 100644 --- a/tests/functional/m/multiple_statements_single_line.txt +++ b/tests/functional/m/multiple_statements_single_line.txt @@ -1,4 +1,4 @@ -multiple-statements:11:9:11:38::More than one statement on a single line:UNDEFINED -multiple-statements:15:9:15:13::More than one statement on a single line:UNDEFINED -multiple-statements:19:9:19:12::More than one statement on a single line:UNDEFINED -multiple-statements:30:26:30:31:MyError:More than one statement on a single line:UNDEFINED +multiple-statements:11:9:11:38::More than one statement on a single line:HIGH +multiple-statements:15:9:15:13::More than one statement on a single line:HIGH +multiple-statements:19:9:19:12::More than one statement on a single line:HIGH +multiple-statements:30:26:30:31:MyError:More than one statement on a single line:HIGH From d98546230310a66642a865992c920fe05e6f0ae0 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 5 Jun 2024 22:50:47 +0200 Subject: [PATCH 3/3] [multiple-statements] Exclude the class with Ellipsis from the check Closes #9398 --- doc/whatsnew/fragments/9398.false_positive | 4 ++++ pylint/checkers/format.py | 9 ++++----- tests/functional/m/multiple_statements.py | 2 +- tests/functional/m/multiple_statements.txt | 1 - 4 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 doc/whatsnew/fragments/9398.false_positive diff --git a/doc/whatsnew/fragments/9398.false_positive b/doc/whatsnew/fragments/9398.false_positive new file mode 100644 index 0000000000..12069d6478 --- /dev/null +++ b/doc/whatsnew/fragments/9398.false_positive @@ -0,0 +1,4 @@ +Classes with only an Ellipsis (``...``) in their body do not trigger 'multiple-statements' +anymore if they are inlined (in accordance with black's 2024 style). + +Closes #9398 diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py index 1e942e3190..f8aecbda64 100644 --- a/pylint/checkers/format.py +++ b/pylint/checkers/format.py @@ -522,9 +522,8 @@ def visit_default(self, node: nodes.NodeNG) -> None: def _check_multi_statement_line(self, node: nodes.NodeNG, line: int) -> None: """Check for lines containing multiple statements.""" - # Do not warn about multiple nested context managers - # in with statements. if isinstance(node, nodes.With): + # Do not warn about multiple nested context managers in with statements. return if ( isinstance(node.parent, nodes.If) @@ -539,10 +538,10 @@ def _check_multi_statement_line(self, node: nodes.NodeNG, line: int) -> None: ): return - # Functions stubs with ``Ellipsis`` as body are exempted. + # Functions stubs and class with ``Ellipsis`` as body are exempted. if ( - isinstance(node.parent, nodes.FunctionDef) - and isinstance(node, nodes.Expr) + isinstance(node, nodes.Expr) + and isinstance(node.parent, (nodes.FunctionDef, nodes.ClassDef)) and isinstance(node.value, nodes.Const) and node.value.value is Ellipsis ): diff --git a/tests/functional/m/multiple_statements.py b/tests/functional/m/multiple_statements.py index 6b90403d5e..cc0a53e111 100644 --- a/tests/functional/m/multiple_statements.py +++ b/tests/functional/m/multiple_statements.py @@ -25,7 +25,7 @@ # (only for classes, not for the examples above) class MyException(Exception): print("Golfing sure is nice") # [multiple-statements] class MyError(Exception): pass # [multiple-statements] -class DebugTrueDetected(Exception): ... # [multiple-statements] +class DebugTrueDetected(Exception): ... class MyError(Exception): a='a' # [multiple-statements] diff --git a/tests/functional/m/multiple_statements.txt b/tests/functional/m/multiple_statements.txt index 3bf4719ea2..6b5be1adf6 100644 --- a/tests/functional/m/multiple_statements.txt +++ b/tests/functional/m/multiple_statements.txt @@ -6,6 +6,5 @@ multiple-statements:15:9:15:13::More than one statement on a single line:HIGH multiple-statements:19:9:19:12::More than one statement on a single line:HIGH multiple-statements:26:30:26:59:MyException:More than one statement on a single line:HIGH multiple-statements:27:26:27:30:MyError:More than one statement on a single line:HIGH -multiple-statements:28:36:28:39:DebugTrueDetected:More than one statement on a single line:HIGH multiple-statements:30:26:30:31:MyError:More than one statement on a single line:HIGH multiple-statements:32:26:32:31:MyError:More than one statement on a single line:HIGH