Skip to content

Commit 192727b

Browse files
[multiple-statements] Make pylint compatible with black's 2024 style (#9697) (#9698)
* Add more test cases to cover pass / ... * Define the confidence as HIGH * Exclude the class with Ellipsis from the check Closes #9398 (cherry picked from commit afd5edf) Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 7e5e4f9 commit 192727b

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Classes with only an Ellipsis (``...``) in their body do not trigger 'multiple-statements'
2+
anymore if they are inlined (in accordance with black's 2024 style).
3+
4+
Closes #9398

pylint/checkers/format.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,8 @@ def visit_default(self, node: nodes.NodeNG) -> None:
522522

523523
def _check_multi_statement_line(self, node: nodes.NodeNG, line: int) -> None:
524524
"""Check for lines containing multiple statements."""
525-
# Do not warn about multiple nested context managers
526-
# in with statements.
527525
if isinstance(node, nodes.With):
526+
# Do not warn about multiple nested context managers in with statements.
528527
return
529528
if (
530529
isinstance(node.parent, nodes.If)
@@ -539,16 +538,16 @@ def _check_multi_statement_line(self, node: nodes.NodeNG, line: int) -> None:
539538
):
540539
return
541540

542-
# Functions stubs with ``Ellipsis`` as body are exempted.
541+
# Functions stubs and class with ``Ellipsis`` as body are exempted.
543542
if (
544-
isinstance(node.parent, nodes.FunctionDef)
545-
and isinstance(node, nodes.Expr)
543+
isinstance(node, nodes.Expr)
544+
and isinstance(node.parent, (nodes.FunctionDef, nodes.ClassDef))
546545
and isinstance(node.value, nodes.Const)
547546
and node.value.value is Ellipsis
548547
):
549548
return
550549

551-
self.add_message("multiple-statements", node=node)
550+
self.add_message("multiple-statements", node=node, confidence=HIGH)
552551
self._visited_lines[line] = 2
553552

554553
def check_trailing_whitespace_ending(self, line: str, i: int) -> None:

tests/functional/m/multiple_statements.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44

55
from typing import overload
66

7+
if True: print("Golfing sure is nice") # [multiple-statements]
78
if True: pass # [multiple-statements]
9+
if True: ... # [multiple-statements]
10+
11+
if True: print("Golfing sure is nice") # [multiple-statements]
12+
else:
13+
pass
814

915
if True: pass # [multiple-statements]
1016
else:
1117
pass
1218

19+
if True: ... # [multiple-statements]
20+
else:
21+
pass
22+
23+
# The following difference in behavior is due to black 2024's style
24+
# that reformat pass on multiple line but reformat "..." on a single line
25+
# (only for classes, not for the examples above)
26+
class MyException(Exception): print("Golfing sure is nice") # [multiple-statements]
1327
class MyError(Exception): pass # [multiple-statements]
28+
class DebugTrueDetected(Exception): ...
1429

1530
class MyError(Exception): a='a' # [multiple-statements]
1631

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
multiple-statements:7:9:7:13::More than one statement on a single line:UNDEFINED
2-
multiple-statements:9:9:9:13::More than one statement on a single line:UNDEFINED
3-
multiple-statements:13:26:13:30:MyError:More than one statement on a single line:UNDEFINED
4-
multiple-statements:15:26:15:31:MyError:More than one statement on a single line:UNDEFINED
5-
multiple-statements:17:26:17:31:MyError:More than one statement on a single line:UNDEFINED
1+
multiple-statements:7:9:7:38::More than one statement on a single line:HIGH
2+
multiple-statements:8:9:8:13::More than one statement on a single line:HIGH
3+
multiple-statements:9:9:9:12::More than one statement on a single line:HIGH
4+
multiple-statements:11:9:11:38::More than one statement on a single line:HIGH
5+
multiple-statements:15:9:15:13::More than one statement on a single line:HIGH
6+
multiple-statements:19:9:19:12::More than one statement on a single line:HIGH
7+
multiple-statements:26:30:26:59:MyException:More than one statement on a single line:HIGH
8+
multiple-statements:27:26:27:30:MyError:More than one statement on a single line:HIGH
9+
multiple-statements:30:26:30:31:MyError:More than one statement on a single line:HIGH
10+
multiple-statements:32:26:32:31:MyError:More than one statement on a single line:HIGH

tests/functional/m/multiple_statements_single_line.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44

55
from typing import overload
66

7+
if True: print("Golfing sure is nice")
78
if True: pass
9+
if True: ...
10+
11+
if True: print("Golfing sure is nice") # [multiple-statements]
12+
else:
13+
pass
814

915
if True: pass # [multiple-statements]
1016
else:
1117
pass
1218

19+
if True: ... # [multiple-statements]
20+
else:
21+
pass
22+
23+
class MyException(Exception): print("Golfing sure is nice")
1324
class MyError(Exception): pass
25+
class DebugTrueDetected(Exception): ...
26+
1427

1528
class MyError(Exception): a='a'
1629

1730
class MyError(Exception): a='a'; b='b' # [multiple-statements]
1831

19-
try:
32+
try: #@
2033
pass
2134
except:
2235
pass
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
multiple-statements:9:9:9:13::More than one statement on a single line:UNDEFINED
2-
multiple-statements:17:26:17:31:MyError:More than one statement on a single line:UNDEFINED
1+
multiple-statements:11:9:11:38::More than one statement on a single line:HIGH
2+
multiple-statements:15:9:15:13::More than one statement on a single line:HIGH
3+
multiple-statements:19:9:19:12::More than one statement on a single line:HIGH
4+
multiple-statements:30:26:30:31:MyError:More than one statement on a single line:HIGH

0 commit comments

Comments
 (0)