Skip to content

Commit

Permalink
[multiple-statements] Make pylint compatible with black's 2024 style (#…
Browse files Browse the repository at this point in the history
…9697)

* 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)
  • Loading branch information
Pierre-Sassoulas authored and github-actions[bot] committed Jun 5, 2024
1 parent 7e5e4f9 commit ab3ac97
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9398.false_positive
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions pylint/checkers/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -539,16 +538,16 @@ 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
):
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:
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/m/multiple_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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): ...

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

Expand Down
15 changes: 10 additions & 5 deletions tests/functional/m/multiple_statements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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: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: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
15 changes: 14 additions & 1 deletion tests/functional/m/multiple_statements_single_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/functional/m/multiple_statements_single_line.txt
Original file line number Diff line number Diff line change
@@ -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: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

0 comments on commit ab3ac97

Please sign in to comment.